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
16 changes: 11 additions & 5 deletions PhpCollective/Sniffs/Classes/Psr4Sniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace PhpCollective\Sniffs\Classes;

use InvalidArgumentException;
use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
Expand Down Expand Up @@ -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;
}
}
Expand Down
25 changes: 25 additions & 0 deletions tests/PhpCollective/Sniffs/Classes/Psr4SniffTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* MIT License
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

namespace PhpCollective\Test\PhpCollective\Sniffs\Classes;

use PhpCollective\Sniffs\Classes\Psr4Sniff;
use PhpCollective\Test\TestCase;

class Psr4SniffTest extends TestCase
{
/**
* @return void
*/
public function testMissingComposerJsonDisablesSniff(): void
{
$sniff = new Psr4Sniff();
$sniff->composerJsonPath = 'does-not-exist/composer.json';

$this->assertSnifferFindsErrors($sniff, 0);
}
}
7 changes: 7 additions & 0 deletions tests/_data/Psr4/before.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types = 1);

namespace PhpCollective;

class FixMe
{
}
Loading