Skip to content

Commit

Permalink
Fix #119 – only discover symbols in PHP files
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHenryIE committed Oct 10, 2024
1 parent 2e0a2f8 commit aaffdbc
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/FileScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public function __construct(StraussConfig $config)
public function findInFiles(DiscoveredFiles $files): DiscoveredSymbols
{
foreach ($files->getFiles() as $file) {
if (!$file->isPhpFile()) {
continue;
}

$this->find($file->getContents(), $file);
}

Expand Down
50 changes: 50 additions & 0 deletions tests/Issues/StraussIssue119Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* `class final` appears in `symfony/console/CHANGELOG.md` causing `symfony/polyfill-php80/Resources/stubs/Attribute.php`
* `final` keyword to be prefixed
*
* @see https://github.com/BrianHenryIE/strauss/issues/119
*/

namespace BrianHenryIE\Strauss\Tests\Issues;

use BrianHenryIE\Strauss\Tests\Integration\Util\IntegrationTestCase;

/**
* @package BrianHenryIE\Strauss\Tests\Issues
* @coversNothing
*/
class StraussIssue119Test extends IntegrationTestCase
{
public function test_muted_errors()
{

$composerJsonString = <<<'EOD'
{
"name": "issue/119",
"require": {
"symfony/polyfill-php80": "*",
"symfony/console": "*"
},
"extra": {
"strauss": {
"namespace_prefix": "Company\\Project\\",
"classmap_prefix": "Company_Project_"
}
}
}
EOD;

chdir($this->testsWorkingDir);

file_put_contents($this->testsWorkingDir . '/composer.json', $composerJsonString);

exec('composer install');

$this->runStrauss();

$php_string = file_get_contents($this->testsWorkingDir . '/vendor-prefixed/symfony/polyfill-php80/Resources/stubs/Attribute.php');

self::assertStringNotContainsString('Company_Project_final class Attribute', $php_string);
}
}
20 changes: 20 additions & 0 deletions tests/Unit/FileScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class MyClass {
$sut = new FileScanner($config);

$file = \Mockery::mock(File::class);
$file->shouldReceive('isPhpFile')->andReturnTrue();
$file->shouldReceive('getContents')->andReturn($validPhp);

$file->shouldReceive('getTargetRelativePath');
Expand Down Expand Up @@ -77,6 +78,7 @@ class MyClass {
$sut = new FileScanner($config);

$file = \Mockery::mock(File::class);
$file->shouldReceive('isPhpFile')->andReturnTrue();
$file->shouldReceive('getContents')->andReturn($validPhp);

$file->shouldReceive('getTargetRelativePath');
Expand Down Expand Up @@ -112,6 +114,7 @@ class MyClass {
$sut = new FileScanner($config);

$file = \Mockery::mock(File::class);
$file->shouldReceive('isPhpFile')->andReturnTrue();
$file->shouldReceive('getContents')->andReturn($validPhp);

$file->shouldReceive('getTargetRelativePath');
Expand Down Expand Up @@ -155,6 +158,7 @@ class MyOtherClass {


$file = \Mockery::mock(File::class);
$file->shouldReceive('isPhpFile')->andReturnTrue();
$file->shouldReceive('getContents')->andReturn($validPhp);

$file->shouldReceive('getTargetRelativePath');
Expand Down Expand Up @@ -212,6 +216,7 @@ public function render($config, $allowed = true, $render_controls = true)

try {
$file = \Mockery::mock(File::class);
$file->shouldReceive('isPhpFile')->andReturnTrue();
$file->shouldReceive('getContents')->andReturn($validPhp);

$file->shouldReceive('getTargetRelativePath');
Expand Down Expand Up @@ -250,6 +255,7 @@ class MyOtherClass {
$sut = new FileScanner($config);

$file = \Mockery::mock(File::class);
$file->shouldReceive('isPhpFile')->andReturnTrue();
$file->shouldReceive('getContents')->andReturn($validPhp);

$file->shouldReceive('getTargetRelativePath');
Expand Down Expand Up @@ -285,6 +291,7 @@ class Whatever {
$fileScanner = new FileScanner($config);

$file = \Mockery::mock(File::class);
$file->shouldReceive('isPhpFile')->andReturnTrue();
$file->shouldReceive('getContents')->andReturn($contents);

$file->shouldReceive('getTargetRelativePath');
Expand Down Expand Up @@ -321,6 +328,7 @@ class Whatever {
$fileScanner = new FileScanner($config);

$file = \Mockery::mock(File::class);
$file->shouldReceive('isPhpFile')->andReturnTrue();
$file->shouldReceive('getContents')->andReturn($contents);

$file->shouldReceive('getTargetRelativePath');
Expand Down Expand Up @@ -360,6 +368,7 @@ class Whatever {
$fileScanner = new FileScanner($config);

$file = \Mockery::mock(File::class);
$file->shouldReceive('isPhpFile')->andReturnTrue();
$file->shouldReceive('getContents')->andReturn($contents);

$file->shouldReceive('getTargetRelativePath');
Expand Down Expand Up @@ -394,6 +403,7 @@ public function test_it_does_not_treat_multiline_comments_on_one_line_as_classes
$fileScanner = new FileScanner($config);

$file = \Mockery::mock(File::class);
$file->shouldReceive('isPhpFile')->andReturnTrue();
$file->shouldReceive('getContents')->andReturn($contents);

$file->shouldReceive('getTargetRelativePath');
Expand Down Expand Up @@ -431,6 +441,7 @@ class Whatever_Ever {
$fileScanner = new FileScanner($config);

$file = \Mockery::mock(File::class);
$file->shouldReceive('isPhpFile')->andReturnTrue();
$file->shouldReceive('getContents')->andReturn($contents);

$file->shouldReceive('getTargetRelativePath');
Expand Down Expand Up @@ -462,6 +473,7 @@ public function test_it_parses_classes_after_semicolon()
$fileScanner = new FileScanner($config);

$file = \Mockery::mock(File::class);
$file->shouldReceive('isPhpFile')->andReturnTrue();
$file->shouldReceive('getContents')->andReturn($contents);

$file->shouldReceive('getTargetRelativePath');
Expand Down Expand Up @@ -496,6 +508,7 @@ class WP_Dependency_Installer {
$fileScanner = new FileScanner($config);

$file = \Mockery::mock(File::class);
$file->shouldReceive('isPhpFile')->andReturnTrue();
$file->shouldReceive('getContents')->andReturn($contents);

$file->shouldReceive('getTargetRelativePath');
Expand Down Expand Up @@ -539,6 +552,7 @@ class B_Class { }
$fileScanner = new FileScanner($config);

$file = \Mockery::mock(File::class);
$file->shouldReceive('isPhpFile')->andReturnTrue();
$file->shouldReceive('getContents')->andReturn($contents);

$file->shouldReceive('getTargetRelativePath');
Expand Down Expand Up @@ -566,6 +580,7 @@ public function testExcludePackagesFromPrefix()
$composerPackage->method('getPackageName')->willReturn('brianhenryie/pdfhelpers');

$file = \Mockery::mock(File::class);
$file->shouldReceive('isPhpFile')->andReturnTrue();
$file->shouldReceive('getContents')->andReturn('');

$files = \Mockery::mock(DiscoveredFiles::class)->makePartial();
Expand All @@ -590,6 +605,7 @@ public function testExcludeFilePatternsFromPrefix()

// $file = new File($composerPackage, 'path/to/file', 'irrelevantPath');
$file = \Mockery::mock(File::class);
$file->shouldReceive('isPhpFile')->andReturnTrue();
$file->shouldReceive('getContents')->andReturn('');

$files = \Mockery::mock(DiscoveredFiles::class)->makePartial();
Expand Down Expand Up @@ -624,6 +640,7 @@ class A_Class { }
$fileScanner = new FileScanner($config);

$file = \Mockery::mock(File::class);
$file->shouldReceive('isPhpFile')->andReturnTrue();
$file->shouldReceive('getContents')->andReturn($contents);

$file->shouldReceive('getTargetRelativePath');
Expand Down Expand Up @@ -676,6 +693,7 @@ public static function objclone($object)
$fileScanner = new FileScanner($config);

$file = \Mockery::mock(File::class);
$file->shouldReceive('isPhpFile')->andReturnTrue();
$file->shouldReceive('getContents')->andReturn($contents);

$file->shouldReceive('getTargetRelativePath');
Expand Down Expand Up @@ -717,6 +735,7 @@ class FPDF
$fileScanner = new FileScanner($config);

$file = \Mockery::mock(File::class);
$file->shouldReceive('isPhpFile')->andReturnTrue();
$file->shouldReceive('getContents')->andReturn($contents);

$file->shouldReceive('getTargetRelativePath');
Expand Down Expand Up @@ -762,6 +781,7 @@ final class WPGraphQL {
$fileScanner = new FileScanner($config);

$file = \Mockery::mock(File::class);
$file->shouldReceive('isPhpFile')->andReturnTrue();
$file->shouldReceive('getContents')->andReturn($contents);

$file->shouldReceive('getTargetRelativePath');
Expand Down

0 comments on commit aaffdbc

Please sign in to comment.