diff --git a/PhpCollective/Sniffs/Classes/Psr4Sniff.php b/PhpCollective/Sniffs/Classes/Psr4Sniff.php index 45c0564..64ad216 100644 --- a/PhpCollective/Sniffs/Classes/Psr4Sniff.php +++ b/PhpCollective/Sniffs/Classes/Psr4Sniff.php @@ -9,6 +9,7 @@ namespace PhpCollective\Sniffs\Classes; +use InvalidArgumentException; use PHP_CodeSniffer\Config; use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; @@ -100,11 +101,16 @@ protected function initializeThisSniffIfNotYet(Config $config): void { if ($this->initialization === static::UNINITIALIZED) { $this->initialization = static::INITIALIZATION_FAILURE; - $this->autoloadabilityInspectors = - AutoloadabilityInspectorsFactory::create( - $config->getSettings()['basepath'] ?: getcwd(), - $this->composerJsonPath, - ); + try { + $this->autoloadabilityInspectors = + AutoloadabilityInspectorsFactory::create( + $config->getSettings()['basepath'] ?: getcwd(), + $this->composerJsonPath, + ); + } catch (InvalidArgumentException) { + // Missing or unreadable composer.json: nothing to check against. Invalid JSON still throws. + return; + } $this->initialization = static::INITIALIZED; } } diff --git a/tests/PhpCollective/Sniffs/Classes/Psr4SniffTest.php b/tests/PhpCollective/Sniffs/Classes/Psr4SniffTest.php new file mode 100644 index 0000000..d46e617 --- /dev/null +++ b/tests/PhpCollective/Sniffs/Classes/Psr4SniffTest.php @@ -0,0 +1,25 @@ +composerJsonPath = 'does-not-exist/composer.json'; + + $this->assertSnifferFindsErrors($sniff, 0); + } +} diff --git a/tests/_data/Psr4/before.php b/tests/_data/Psr4/before.php new file mode 100644 index 0000000..d5f03cd --- /dev/null +++ b/tests/_data/Psr4/before.php @@ -0,0 +1,7 @@ +