diff --git a/build/psalm/StaticVarsChecker.php b/build/psalm/StaticVarsChecker.php new file mode 100644 index 00000000000..24d08b45aa8 --- /dev/null +++ b/build/psalm/StaticVarsChecker.php @@ -0,0 +1,54 @@ +getStmt(); + $statementsSource = $event->getStatementsSource(); + + foreach ($classLike->stmts as $stmt) { + if ($stmt instanceof Property) { + if ($stmt->isStatic()) { + IssueBuffer::maybeAdd( + // ImpureStaticProperty is close enough, all static properties are impure to my eyes + new \Psalm\Issue\ImpureStaticProperty( + 'Static property should not be used as they do not follow requests lifecycle', + new CodeLocation($statementsSource, $stmt), + ) + ); + } + } + } + } + + public static function afterStatementAnalysis(AfterStatementAnalysisEvent $event): ?bool { + $stmt = $event->getStmt(); + if ($stmt instanceof PhpParser\Node\Stmt\Static_) { + IssueBuffer::maybeAdd( + // Same logic + new \Psalm\Issue\ImpureStaticVariable( + 'Static var should not be used as they do not follow requests lifecycle and are hard to reset', + new CodeLocation($event->getStatementsSource(), $stmt), + ) + ); + } + return null; + } +} diff --git a/build/psalm/StaticVarsTest.php b/build/psalm/StaticVarsTest.php new file mode 100644 index 00000000000..f40ac56ff51 --- /dev/null +++ b/build/psalm/StaticVarsTest.php @@ -0,0 +1,23 @@ + +