Skip to content

Commit

Permalink
Fix false-positive when FQN reference of non-lowercase function is us…
Browse files Browse the repository at this point in the history
…ed (#90)
  • Loading branch information
janedbal committed Mar 5, 2024
1 parent e7db687 commit b67036e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ShipMonk\ComposerDependencyAnalyser\Result\AnalysisResult;
use ShipMonk\ComposerDependencyAnalyser\Result\SymbolUsage;
use UnexpectedValueException;
use function array_change_key_case;
use function array_diff;
use function array_filter;
use function array_key_exists;
Expand All @@ -38,8 +39,10 @@
use function str_replace;
use function strlen;
use function strpos;
use function strtolower;
use function substr;
use function trim;
use const CASE_LOWER;
use const DIRECTORY_SEPARATOR;

class Analyser
Expand Down Expand Up @@ -127,7 +130,7 @@ public function run(): AnalysisResult
$scannedFilesCount++;

foreach ($this->getUsedSymbolsInFile($filePath) as $usedSymbol => $lineNumbers) {
if (isset($this->ignoredSymbols[$usedSymbol])) {
if (isset($this->ignoredSymbols[strtolower($usedSymbol)])) {
continue;
}

Expand Down Expand Up @@ -497,7 +500,7 @@ private function getIgnoredSymbols(): array
}
}

return $ignoredSymbols;
return array_change_key_case($ignoredSymbols, CASE_LOWER); // get_defined_functions returns lowercase functions
}

}
23 changes: 23 additions & 0 deletions tests/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,29 @@ public function testDevPathInsideProdPath(): void
self::assertEquals($this->createAnalysisResult(2, []), $result);
}

public function testOtherSymbols(): void
{
require_once __DIR__ . '/data/not-autoloaded/other-symbols/symbol-declaration.php';

$vendorDir = realpath(__DIR__ . '/../vendor');
$path = realpath(__DIR__ . '/data/not-autoloaded/other-symbols/symbol-usages.php');
self::assertNotFalse($vendorDir);
self::assertNotFalse($path);

$config = new Configuration();
$config->addPathToScan($path, false);

$detector = new Analyser(
$this->getStopwatchMock(),
[$vendorDir => $this->getClassLoaderMock()],
$config,
[]
);
$result = $detector->run();

self::assertEquals($this->createAnalysisResult(1, []), $result);
}

public function testPharSupport(): void
{
$canCreatePhar = ini_set('phar.readonly', '0');
Expand Down
6 changes: 3 additions & 3 deletions tests/data/not-autoloaded/analysis/various-usages.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
new DevClazz(); // reported as dev
new DateTimeImmutable();

echo \DIRECTORY_SEPARATOR;
echo \strlen('');

\Composer\InstalledVersions::getInstalledPackages();




ShadowClazz::class;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace acme {
define('one', 'lower');
define('ONE', 'upper');
function camelCase(): void {}
}


14 changes: 14 additions & 0 deletions tests/data/not-autoloaded/other-symbols/symbol-usages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use const PHP_EOL;
use const acme\one;

echo PHP_EOL;
echo \DIRECTORY_SEPARATOR;
echo \strlen('');
echo substr(' ', 0, 1);

\acme\camelCase();
echo one;
echo ONE;

0 comments on commit b67036e

Please sign in to comment.