diff --git a/composer.json b/composer.json index c8520a3..c5b7760 100644 --- a/composer.json +++ b/composer.json @@ -13,19 +13,19 @@ } ], "require": { - "php": ">=7.1", - "psr/simple-cache": "^1", + "php": ">=8.1", + "psr/simple-cache": "^1 || ^3", "mouf/classname-mapper": "^1", "ext-hash": "*" }, "require-dev": { - "php-coveralls/php-coveralls": "^2.1", - "phpunit/phpunit": "^7.2.7", - "squizlabs/php_codesniffer": "^3.2.3", - "phpstan/phpstan": "^0.12", - "maglnet/composer-require-checker": "^1.0", - "thecodingmachine/phpstan-strict-rules": "^0.12", - "symfony/cache": "^4.1.4" + "php-coveralls/php-coveralls": "^2.1||^2.7", + "phpunit/phpunit": "^7.2.7 || ^10.5 || ^11.0", + "squizlabs/php_codesniffer": "^3.2.3 || ^3.9", + "phpstan/phpstan": "^0.12 || ^1.10", + "maglnet/composer-require-checker": "^1.0 || ^4.10", + "thecodingmachine/phpstan-strict-rules": "^0.12 || ^1.0", + "symfony/cache": "^4.1.4 || ^7.0" }, "autoload": { "psr-4": { diff --git a/phpstan.neon b/phpstan.neon index 9eaac43..7acf670 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,4 @@ parameters: - ignoreErrors: - - "/Parameter #1 $directory of function chdir expects string, string|false given./" + level: 8 includes: - vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a82002a..d3848a8 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,27 +1,20 @@ - - - - - ./tests/ - - - - - src - - - - - - + + + + + + + + + + ./tests/ + + + + + + src + + diff --git a/src/Glob/GlobClassExplorer.php b/src/Glob/GlobClassExplorer.php index 0c08c32..31b3dbd 100644 --- a/src/Glob/GlobClassExplorer.php +++ b/src/Glob/GlobClassExplorer.php @@ -3,10 +3,10 @@ namespace TheCodingMachine\ClassExplorer\Glob; +use Psr\SimpleCache\InvalidArgumentException; use SplFileInfo; use function array_keys; use function chdir; -use DirectoryIterator; use GlobIterator; use Mouf\Composer\ClassNameMapper; use Psr\SimpleCache\CacheInterface; @@ -14,7 +14,6 @@ use RecursiveIteratorIterator; use RegexIterator; use TheCodingMachine\ClassExplorer\ClassExplorerInterface; -use function var_dump; /** * Returns a set of classes by analyzing the PHP files in a directory. @@ -30,34 +29,13 @@ */ class GlobClassExplorer implements ClassExplorerInterface { - /** - * @var string - */ - private $namespace; - /** - * @var CacheInterface - */ - private $cache; - /** - * @var int|null - */ - private $cacheTtl; - /** - * @var ClassNameMapper|null - */ - private $classNameMapper; - /** - * @var bool - */ - private $recursive; - /** - * @var string - */ - private $rootPath; - /** - * @var string|null - */ - private $key; + private string $namespace; + private CacheInterface $cache; + private ?int $cacheTtl = null; + private ?ClassNameMapper $classNameMapper = null; + private bool $recursive; + private string $rootPath; + private ?string $key = null; public function __construct(string $namespace, CacheInterface $cache, ?int $cacheTtl = null, ?ClassNameMapper $classNameMapper = null, bool $recursive = true, ?string $rootPath = null) { @@ -89,11 +67,20 @@ public function getClassMap(): array if ($this->key === null) { $this->key = 'globClassExplorer_'.hash('md4', $this->namespace.'___'.$this->recursive.$this->rootPath); } - $classes = $this->cache->get($this->key); + try { + $classes = $this->cache->get($this->key); + } catch (InvalidArgumentException $e) { + $classes = null; + } if ($classes === null) { $classes = $this->doGetClassMap(); - $this->cache->set($this->key, $classes, $this->cacheTtl); + try { + $this->cache->set($this->key, $classes, $this->cacheTtl); + } catch (InvalidArgumentException $e) { + // @ignoreException + } } + return $classes; } @@ -124,6 +111,7 @@ private function doGetClassMap(): array $classes[$namespace.\str_replace('/', '\\', $fileTrimPrefixSuffix)] = $file->getRealPath(); } } + /* @phpstan-ignore-next-line */ chdir($oldCwd); return $classes; } diff --git a/tests/Glob/GlobClassExplorerTest.php b/tests/Glob/GlobClassExplorerTest.php index 0cd1565..4e4d2ff 100644 --- a/tests/Glob/GlobClassExplorerTest.php +++ b/tests/Glob/GlobClassExplorerTest.php @@ -4,22 +4,23 @@ use Mouf\Composer\ClassNameMapper; use PHPUnit\Framework\TestCase; -use Symfony\Component\Cache\Simple\NullCache; +use Symfony\Component\Cache\Adapter\ArrayAdapter; +use Symfony\Component\Cache\Psr16Cache; use TheCodingMachine\ClassExplorer\ClassExplorerInterface; class GlobClassExplorerTest extends TestCase { public function testGetClasses() { - $explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\', new NullCache(), null, null, true, __DIR__.'/../..'); + $explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\', new Psr16Cache(new ArrayAdapter()), null, null, true, __DIR__.'/../..'); $classes = $explorer->getClasses(); - $this->assertSame([GlobClassExplorer::class, ClassExplorerInterface::class], $classes); + $this->assertSame([ClassExplorerInterface::class, GlobClassExplorer::class], $classes); } public function testGetClassesNonRecursive() { - $explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\', new NullCache(), null, null, false, __DIR__.'/../..'); + $explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\', new Psr16Cache(new ArrayAdapter()), null, null, false, __DIR__.'/../..'); $classes = $explorer->getClasses(); $this->assertSame([ClassExplorerInterface::class], $classes); @@ -27,7 +28,7 @@ public function testGetClassesNonRecursive() public function testGetDevClasses() { - $explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\Glob\\', new NullCache(), null, ClassNameMapper::createFromComposerFile(null, null, true), true, __DIR__.'/../..'); + $explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\Glob\\', new Psr16Cache(new ArrayAdapter()), null, ClassNameMapper::createFromComposerFile(null, null, true), true, __DIR__.'/../..'); $classes = $explorer->getClasses(); $this->assertSame([GlobClassExplorer::class, GlobClassExplorerTest::class], $classes); @@ -35,7 +36,7 @@ public function testGetDevClasses() public function testGetNotExistingClasses() { - $explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\Glob\\Foobar\\', new NullCache(), null, null, true, __DIR__.'/../..'); + $explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\Glob\\Foobar\\', new Psr16Cache(new ArrayAdapter()), null, null, true, __DIR__.'/../..'); $classes = $explorer->getClasses(); $this->assertSame([], $classes); @@ -43,7 +44,7 @@ public function testGetNotExistingClasses() public function testGetClassMap() { - $explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\', new NullCache(), null, null, true, __DIR__.'/../..'); + $explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\', new Psr16Cache(new ArrayAdapter()), null, null, true, __DIR__.'/../..'); $classMap = $explorer->getClassMap(); $this->assertArrayHasKey(GlobClassExplorer::class, $classMap);