Skip to content

Commit b316164

Browse files
committed
Rule was seeing every table reference as a table creation
1 parent 43eae43 commit b316164

3 files changed

Lines changed: 59 additions & 4 deletions

File tree

src/Rules/Phinx/ForbidMultipleTableCreationsRule.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ final class ForbidMultipleTableCreationsRule extends PhinxRule
2323
. 'Fix: split into one migration per table.';
2424

2525
/**
26-
* @var array<string, int>
26+
* @var array<string, array<string, true>>
2727
*/
28-
private array $tableCallsPerClass = [];
28+
private array $tableNamesPerClass = [];
2929

3030
public function getNodeType(): string
3131
{
@@ -42,15 +42,20 @@ public function processNode(Node $node, Scope $scope): array
4242
return [];
4343
}
4444

45+
$tableName = $this->extractTableName($node);
46+
if ($tableName === null) {
47+
return [];
48+
}
49+
4550
$classReflection = $scope->getClassReflection();
4651
if ($classReflection === null) {
4752
return [];
4853
}
4954

5055
$className = $classReflection->getName();
51-
$this->tableCallsPerClass[$className] = ($this->tableCallsPerClass[$className] ?? 0) + 1;
56+
$this->tableNamesPerClass[$className][$tableName] = true;
5257

53-
if ($this->tableCallsPerClass[$className] > 1) {
58+
if (count($this->tableNamesPerClass[$className]) > 1) {
5459
return [
5560
RuleErrorBuilder::message(self::MESSAGE)
5661
->identifier(self::RULE_IDENTIFIER)
@@ -66,4 +71,22 @@ private function isTableCall(MethodCall $node): bool
6671
return $node->name instanceof Identifier
6772
&& $node->name->toString() === 'table';
6873
}
74+
75+
private function extractTableName(MethodCall $node): ?string
76+
{
77+
if (count($node->args) === 0) {
78+
return null;
79+
}
80+
81+
$firstArg = $node->args[0];
82+
if (!$firstArg instanceof Node\Arg) {
83+
return null;
84+
}
85+
86+
if ($firstArg->value instanceof Node\Scalar\String_) {
87+
return $firstArg->value->value;
88+
}
89+
90+
return null;
91+
}
6992
}

tests/Rules/Phinx/ForbidMultipleTableCreationsRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ public function testAllowsSingleTableCreateInMigration(): void
3939
);
4040
}
4141

42+
public function testAllowsSameTableReferencedMultipleTimes(): void
43+
{
44+
$this->analyse(
45+
[__DIR__ . '/fixtures/SameTableMultipleReferences.php'],
46+
[]
47+
);
48+
}
49+
4250
public function testDoesNotReportOutsidePhinxMigration(): void
4351
{
4452
$this->analyse(
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpStanMigrationRules\Tests\Rules\Phinx\Fixtures;
6+
7+
use Phinx\Migration\AbstractMigration;
8+
9+
final class SameTableMultipleReferences extends AbstractMigration
10+
{
11+
public function change(): void
12+
{
13+
if ($this->table('users')->hasColumn('nickname')) {
14+
return;
15+
}
16+
17+
$this->table('users')
18+
->addColumn('nickname', 'string', [
19+
'default' => '',
20+
'null' => false,
21+
])
22+
->update();
23+
}
24+
}

0 commit comments

Comments
 (0)