forked from shopware/shopware
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'next-30847/prevent-after-statements-phpstan-rule' into …
…'trunk' NEXT-30847 - Add AFTER statement rule See merge request shopware/6/product/platform!11587
- Loading branch information
Showing
3 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
.../_unreleased/2023-10-12-phpstan-rule-to-prevent-after-statement-in-migration.md
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,9 @@ | ||
--- | ||
title: phpstan-rule-to-prevent-after-statement-in-migration | ||
issue: NEXT-30847 | ||
author: Alexandru Dumea | ||
author_email: [email protected] | ||
author_github: Alexandru Dumea | ||
--- | ||
# Core | ||
* Added a PHPStan rule, NoAfterStatementRule.php, to prevent using AFTER statement in migrations |
77 changes: 77 additions & 0 deletions
77
src/Core/DevOps/StaticAnalyze/PHPStan/Rules/NoAfterStatementRule.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,77 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Shopware\Core\DevOps\StaticAnalyze\PHPStan\Rules; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use Shopware\Core\Framework\Log\Package; | ||
use Shopware\Core\Framework\Migration\MigrationStep; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @implements Rule<MethodCall> | ||
*/ | ||
#[Package('core')] | ||
class NoAfterStatementRule implements Rule | ||
{ | ||
private const CUTOFF_UNIX_TIMESTAMP = '2023-10-11 00:00:00'; | ||
|
||
public function getNodeType(): string | ||
{ | ||
return MethodCall::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!$node instanceof MethodCall) { | ||
return []; | ||
} | ||
|
||
if (!$this->isRecentMigration($scope)) { | ||
return []; | ||
} | ||
|
||
if (!$node->name instanceof Node\Identifier) { | ||
return []; | ||
} | ||
|
||
if (empty($node->getArgs())) { | ||
return []; | ||
} | ||
|
||
$arg = $node->getArgs()[0]->value; | ||
if (!$arg instanceof Node\Scalar\String_) { | ||
return []; | ||
} | ||
|
||
$pattern = '/ALTER\s+TABLE\s+.+?\s+ADD\s+.+?\s+AFTER\s+`?[a-zA-Z0-9_]+`?/i'; | ||
|
||
if (preg_match($pattern, $arg->value)) { | ||
return ['Usage of ALTER TABLE .. AFTER is disallowed in migrations to avoid implicit temporary table usage.']; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
private function isRecentMigration(Scope $scope): bool | ||
{ | ||
$class = $scope->getClassReflection(); | ||
if (!$class || !$class->isSubclassOf(MigrationStep::class)) { | ||
return false; | ||
} | ||
|
||
$className = substr($class->getName(), strrpos($class->getName(), '\\') + 1); | ||
|
||
if (preg_match('/Migration(\d{10})/', $className, $matches)) { | ||
$migrationUnixTimestamp = (int) $matches[1]; | ||
$cutoffUnixTimestamp = strtotime(self::CUTOFF_UNIX_TIMESTAMP); | ||
|
||
return $migrationUnixTimestamp > $cutoffUnixTimestamp; | ||
} | ||
|
||
return false; | ||
} | ||
} |
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