-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRequireWithoutTimestampsRule.php
58 lines (48 loc) · 1.31 KB
/
RequireWithoutTimestampsRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
declare(strict_types=1);
namespace Worksome\CodingStyle\PHPStan\Laravel\Migrations;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PHPStan\Analyser\Scope;
use PHPStan\Node\FileNode;
use PHPStan\Rules\Rule;
/**
* Custom PHPStan rule to check if migration files using 'update' or 'insert'
* methods include 'withoutTimestamps' somewhere in the file.
*/
readonly class RequireWithoutTimestampsRule implements Rule
{
public const TIMESTAMP_MODIFYING_METHODS = [
'update',
'insert',
'save',
'saveQuietly',
'delete',
'restore',
];
public function __construct(private string $migrationsPath = 'database/migrations')
{
}
public function getNodeType(): string
{
return FileNode::class;
}
/**
* @param FileNode $node
* @param Scope $scope
*
* @return string[] Errors
*/
public function processNode(Node $node, Scope $scope): array
{
$filePath = $scope->getFile();
if (! str_contains($filePath, $this->migrationsPath)) {
return [];
}
$traverser = new NodeTraverser();
$visitor = new WithoutTimestampsVisitor($scope);
$traverser->addVisitor($visitor);
$traverser->traverse($node->getNodes());
return $visitor->errors;
}
}