-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: JIRA-13731 Allow supporting database path
- Loading branch information
1 parent
f893ab8
commit fee91f0
Showing
6 changed files
with
140 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/PHPStan/Laravel/Migrations/WithoutTimestampsVisitor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Worksome\CodingStyle\PHPStan\Laravel\Migrations; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\NodeVisitorAbstract; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
class WithoutTimestampsVisitor extends NodeVisitorAbstract | ||
{ | ||
private array $contextStack = []; | ||
public array $errors = []; | ||
|
||
public function enterNode(Node $node) | ||
{ | ||
// Track context for 'withoutTimestamps' method calls | ||
if ($this->isWithoutTimestampsCall($node)) { | ||
$this->contextStack[] = 'withoutTimestamps'; | ||
} else { | ||
$this->contextStack[] = null; | ||
} | ||
|
||
if ($this->isUpdateOrInsertCall($node)) { | ||
if (!$this->isWithinWithoutTimestampsContext() && !$this->hasWithoutTimestampsChain($node)) { | ||
$this->errors[] = RuleErrorBuilder::message( | ||
sprintf( | ||
"Line %d: The '%s()' method call should be within 'withoutTimestamps()' context to prevent unintended timestamp updates.", | ||
$node->getLine(), | ||
$node->name->name | ||
) | ||
)->line($node->getLine())->build(); | ||
} | ||
} | ||
} | ||
|
||
public function leaveNode(Node $node): void | ||
{ | ||
array_pop($this->contextStack); | ||
} | ||
|
||
private function isWithoutTimestampsCall(Node $node): bool | ||
{ | ||
return ($node instanceof MethodCall || $node instanceof StaticCall) | ||
&& $node->name instanceof Node\Identifier | ||
&& $node->name->name === 'withoutTimestamps'; | ||
} | ||
|
||
private function isUpdateOrInsertCall(Node $node): bool | ||
{ | ||
return ($node instanceof MethodCall || $node instanceof StaticCall) | ||
&& $node->name instanceof Node\Identifier | ||
&& in_array($node->name->name, ['update', 'insert', 'save', 'saveQuietly', 'create'], true); | ||
} | ||
|
||
private function isWithinWithoutTimestampsContext(): bool | ||
{ | ||
return in_array('withoutTimestamps', $this->contextStack); | ||
} | ||
|
||
private function hasWithoutTimestampsChain(Node $node): bool | ||
{ | ||
$currentNode = $node; | ||
|
||
while ($currentNode instanceof MethodCall || $currentNode instanceof StaticCall) { | ||
if ($currentNode->name instanceof Node\Identifier | ||
&& $currentNode->name->name === 'withoutTimestamps') { | ||
return true; | ||
} | ||
|
||
$currentNode = $currentNode instanceof MethodCall | ||
? $currentNode->var | ||
: ($currentNode instanceof StaticCall ? $currentNode->class : null); | ||
} | ||
|
||
return false; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...tTimestampRule/Fixture/database/migrations/invalid_migration_with_multiple_models.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::table('users', function (Blueprint $table) { | ||
$table->string('test', 50) | ||
->nullable(); | ||
}); | ||
|
||
Client::withTrashed()->update([ | ||
'test' => 'test', | ||
]); | ||
|
||
User::withoutTimestamps(function(){ | ||
User::withTrashed() ->update([ | ||
'test' => 'test', | ||
]); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters