Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bump dependencies and actualise codebase #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 1 addition & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -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
43 changes: 18 additions & 25 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="Test suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage>
<report>
<clover outputFile="build/logs/clover.xml"/>
<html outputDirectory="build/coverage"/>
</report>
</coverage>
<testsuites>
<testsuite name="Test suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<logging/>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
52 changes: 20 additions & 32 deletions src/Glob/GlobClassExplorer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@

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;
use RecursiveDirectoryIterator;
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.
Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -124,6 +111,7 @@ private function doGetClassMap(): array
$classes[$namespace.\str_replace('/', '\\', $fileTrimPrefixSuffix)] = $file->getRealPath();
}
}
/* @phpstan-ignore-next-line */
chdir($oldCwd);
return $classes;
}
Expand Down
15 changes: 8 additions & 7 deletions tests/Glob/GlobClassExplorerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,47 @@

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);
}

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);
}

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);
}

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);
Expand Down