Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Analyser/FileAnalysisProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,20 @@ private function fileState(array $nodes): array

if ($this->isSymbolDeclaration($node)) {
$declaresSymbols = true;

// A define() argument can itself be effectful, e.g. define('X', include 'config.php').
if ($node instanceof Expression) {
$argumentSideEffectLine = $this->intrinsicSideEffectLineInExpression($node->expr);

if ($argumentSideEffectLine !== null) {
if (! $hasSideEffects) {
$sideEffectLine = $argumentSideEffectLine;
}

$hasSideEffects = true;
}
}

continue;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/Analyser/FileAnalysisProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,13 @@ public static function defineStatementProvider(): iterable
yield 'define() method call' => ["\$container->define('X', 1);", false, true];
yield 'defined() on a variable' => ["\$guard || define('X', 1);", false, true];
yield 'assignment' => ['$version = 1;', false, true];
yield 'define() with include argument' => ["define('X', include 'config.php');", true, true];
yield 'define() with assignment argument' => ["define('X', \$config = 1);", true, true];
yield 'guarded define() with include argument' => [
"defined('X') || define('X', include 'config.php');",
true,
true,
];
}

#[DataProvider('defineStatementProvider')]
Expand Down
25 changes: 25 additions & 0 deletions tests/Rule/File/Psr1SymbolsOrSideEffectsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,31 @@ public function testViolatesEchoNextToDefineAndClass(): void
}
}

public function testViolatesDefineWithSideEffectArgumentNextToClass(): void
{
$basePath = $this->makeTempDir();

try {
mkdir($basePath . '/src');
file_put_contents(
$basePath . '/src/Foo.php',
"<?php\ndefine('APP_CONFIG', include 'config.php');\nclass Bootstrap {}\n"
);

$violations = (new Psr1SymbolsOrSideEffectsRule(['src/']))->evaluateProjectAll(
$basePath,
Architecture::define()
);

$this->assertCount(1, $violations);
$this->assertSame(2, $violations[0]->line);
} finally {
unlink($basePath . '/src/Foo.php');
rmdir($basePath . '/src');
rmdir($basePath);
}
}

public function testViolatesDefineMixedWithSideEffectCall(): void
{
$basePath = $this->makeTempDir();
Expand Down
Loading