Skip to content

Commit

Permalink
Merge pull request #114 from koriym/bp
Browse files Browse the repository at this point in the history
Update PHP versions in CI workflow and update QA tools latest
  • Loading branch information
koriym authored Nov 27, 2024
2 parents 69ad4bf + df0fe4f commit 2fbbb13
Show file tree
Hide file tree
Showing 46 changed files with 645 additions and 2,398 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ jobs:
ci:
uses: ray-di/.github/.github/workflows/continuous-integration.yml@v1
with:
old_stable: '["7.2", "7.3", "7.4", "8.0", "8.1", "8.2"]'
current_stable: 8.3
old_stable: '["7.2", "7.3", "7.4", "8.0", "8.1", "8.2", "8.3"]'
current_stable: 8.4
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@
"cs": ["phpcs --standard=./phpcs.xml src tests"],
"cs-fix": ["phpcbf src tests"],
"clean": ["phpstan clear-result-cache", "psalm --clear-cache", "rm -rf tests/tmp/*.php"],
"sa": ["psalm -c psalm.compiler.xml --show-info=true", "phpstan analyse -c phpstan.neon --no-progress"],
"sa": ["psalm --show-info=false", "phpstan analyse -c phpstan.neon --no-progress"],
"metrics": ["@test", "phpmetrics --report-html=build/metrics --exclude=Exception --log-junit=build/junit.xml --junit=build/junit.xml src"],
"phpmd": ["phpmd src/di text ./phpmd.xml"],
"phpmd": ["phpmd src text ./phpmd.xml"],
"build": ["@cs", "@sa", "@pcov", "@metrics"]
},
"extra": {
Expand Down
7 changes: 5 additions & 2 deletions docs/exmaple/ProdInjectorContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Doctrine\Common\Cache\CacheProvider;
use Ray\Compiler\AbstractInjectorContext;
use Ray\Compiler\DiCompileModule;
use Ray\Compiler\FakeCarModule;
use Ray\Di\AbstractModule;

final class ProdInjectorContext extends AbstractInjectorContext
Expand All @@ -23,6 +22,10 @@ public function __invoke(): AbstractModule

public function getCache(): CacheProvider
{
return new ApcuCache();
if (! class_exists(ApcuCache::class)) {
throw new \RuntimeException('doctrine/cache ^1.0 is required for ProdInjectorContext.');
}

return new ApcuCache(); // @phpstan-ignore-line
}
}
19 changes: 10 additions & 9 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
parameters:
level: max
paths:
- src
- tests
excludePaths:
- tests/tmp/*
- tests/Fake/*
checkGenericClassInNonGenericObjectType: false

level: 8
paths:
- src
- tests
excludePaths:
- tests/tmp/*
- tests/Fake/*
ignoreErrors:
-
identifier: typeAlias.duplicate
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="tests/bootstrap.php">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" bootstrap="tests/bootstrap.php">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
Expand Down
15 changes: 0 additions & 15 deletions psalm.compiler.xml

This file was deleted.

4 changes: 1 addition & 3 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?xml version="1.0"?>
<psalm
errorLevel="2"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor-bin/tools/vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
Expand Down
2 changes: 1 addition & 1 deletion src-deprecated/ReaderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @deprecated
* @codeCoverageIgnore
*/
final class ReaderProvider implements ProviderInterface
final class ReaderProvider implements ProviderInterface // @phpstan-ignore-line
{
public function get(): Reader
{
Expand Down
5 changes: 4 additions & 1 deletion src/AbstractInjectorContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@

use Doctrine\Common\Cache\CacheProvider;
use Ray\Di\AbstractModule;
use Ray\Di\Annotation\ScriptDir;

/** @psalm-import-type ScriptDir from CompileInjector */
abstract class AbstractInjectorContext implements LazyModuleInterface
{
/**
* @var string
* @var ScriptDir
* @readonly
*/
public $tmpDir;

/** @param ScriptDir $tmpDir */
public function __construct(string $tmpDir)
{
$this->tmpDir = $tmpDir;
Expand Down
4 changes: 1 addition & 3 deletions src/Annotation/Compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

use Ray\Di\Di\Qualifier;

/**
* @Qualifier
*/
/** @Qualifier */
final class Compile
{
}
5 changes: 5 additions & 0 deletions src/CachedInjectorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

use Doctrine\Common\Cache\CacheProvider;
use Ray\Di\AbstractModule;
use Ray\Di\Annotation\ScriptDir;
use Ray\Di\InjectorInterface;
use Ray\Di\NullCache;

use function assert;
use function serialize;
use function unserialize;

/** @psalm-import-type ScriptDir from CompileInjector */
final class CachedInjectorFactory
{
/** @var array<string, string> */
Expand All @@ -21,6 +23,7 @@ final class CachedInjectorFactory
/**
* @param callable(): AbstractModule $modules
* @param array<class-string> $savedSingletons
* @param ScriptDir $scriptDir
*/
public static function getInstance(string $injectorId, string $scriptDir, callable $modules, ?CacheProvider $cache = null, array $savedSingletons = []): InjectorInterface
{
Expand Down Expand Up @@ -53,6 +56,7 @@ public static function getInstance(string $injectorId, string $scriptDir, callab
/**
* @param callable(): AbstractModule $modules
* @param array<class-string> $savedSingletons
* @param ScriptDir $scriptDir
*/
public static function getOverrideInstance(
string $scriptDir,
Expand All @@ -66,6 +70,7 @@ public static function getOverrideInstance(
/**
* @param callable(): AbstractModule $modules
* @param array<class-string> $savedSingletons
* @param ScriptDir $scriptDir
*/
private static function getInjector(callable $modules, string $scriptDir, array $savedSingletons, ?AbstractModule $module = null): InjectorInterface
{
Expand Down
4 changes: 1 addition & 3 deletions src/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ final class Code
/** @var Node */
private $node;

/**
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
/** @SuppressWarnings(PHPMD.BooleanArgumentFlag) // @phpstan-ignore-line */
public function __construct(Node $node, bool $isSingleton = false, ?IpQualifier $qualifier = null)
{
$this->node = $node;
Expand Down
78 changes: 53 additions & 25 deletions src/CompileInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
use Ray\Compiler\Exception\Unbound;
use Ray\Di\Annotation\ScriptDir;
use Ray\Di\Bind;
use Ray\Di\InjectorInterface;
use Ray\Di\Name;
use ReflectionParameter;

use function assert;
use function file_exists;
use function in_array;
use function rtrim;
Expand All @@ -18,77 +20,107 @@
use function str_replace;
use function touch;

final class CompileInjector implements ScriptInjectorInterface
/**
* Compile Injector
*
* This injector will compile all bindings into PHP's low-code if they have not already been compiled.
* Once compiled, unknown concrete classes will not be compiled at runtime like ScriptInjector.
* All bindings must be explicitly pre-compiled.
*
* @psalm-type ScriptDir = non-empty-string
* @psalm-type Ip = array{0: string, 1: string, 2: string}
* @psalm-type Singletons = array<string, object>
* @psalm-type Prottype = callable(string, Ip): mixed
* @psalm-type Singleton = callable(string, Ip): mixed
* @psalm-type InjectionPoint = callable(): InjectionPoint
* @psalm-type Injector = callable(): InjectorInterface
* @psalm-type InstanceFunctions = array{0: Prottype, 1: Singleton, 2: InjectionPoint, 3: Injector}
* @psalm-type ScriptDirs = list<ScriptDir>
*/
final class CompileInjector implements ScriptInjectorInterface // @phpstan-ignore-line
{
public const INSTANCE = '%s/%s.php';
public const COMPILE_CHECK = '%s/compiled';

/** @var string */
/** @var ScriptDir */
private $scriptDir;

/**
* Injection Point
*
* [$class, $method, $parameter]
*
* @var array{0: string, 1: string, 2: string}
* @var Ip
*/
private $ip = ['', '', ''];

/**
* Singleton instance container
*
* @var array<object>
* @var Singletons
*/
private $singletons = [];

/** @var array<callable> */
private $functions;
/** @var InstanceFunctions */
private $functions; // @phpstan-ignore-line

/** @var LazyModuleInterface */
private $lazyModule;

/** @var array<string> */
/** @var ScriptDirs */
private static $scriptDirs = [];

/**
* @param string $scriptDir generated instance script folder path
* @param ScriptDir $scriptDir generated instance script folder path
* @param LazyModuleInterface $lazyModule callable variable which return AbstractModule instance
*
* @psalm-suppress UnresolvableInclude
*/
public function __construct($scriptDir, LazyModuleInterface $lazyModule)
public function __construct(string $scriptDir, LazyModuleInterface $lazyModule)
{
$this->scriptDir = rtrim($scriptDir, '/');
$this->init($scriptDir, $lazyModule);
}

/** @param ScriptDir $scriptDir */
public function init(string $scriptDir, LazyModuleInterface $lazyModule): void
{
/** @var ScriptDir $scriptDir */
$scriptDir = rtrim($scriptDir, '/');
$this->scriptDir = $scriptDir;
$this->lazyModule = $lazyModule;
$this->registerLoader();
$prototype =
/**
* @param array{0: string, 1: string, 2: string} $injectionPoint
* @param Ip $injectionPoint
*
* @return mixed
*/
function (string $dependencyIndex, array $injectionPoint = ['', '', '']) {
$this->ip = $injectionPoint; // @phpstan-ignore-line
[$prototype, $singleton, $injectionPoint, $injector] = $this->functions;
$instancFile = $this->getInstanceFile($dependencyIndex);
assert(file_exists($instancFile), new Unbound($dependencyIndex));

return require $this->getInstanceFile($dependencyIndex);
return require $instancFile;
};
$singleton =
/**
* @param array{0: string, 1: string, 2: string} $injectionPoint
* @param Ip $injectionPoint
*
* @return mixed
*/
function (string $dependencyIndex, $injectionPoint = ['', '', '']) {
function (string $dependencyIndex, array $injectionPoint = ['', '', '']) {
if (isset($this->singletons[$dependencyIndex])) {
return $this->singletons[$dependencyIndex];
}

$this->ip = $injectionPoint;
$this->ip = $injectionPoint; // @phpstan-ignore-line
[$prototype, $singleton, $injectionPoint, $injector] = $this->functions;

$instance = require $this->getInstanceFile($dependencyIndex);
$instanceFile = $this->getInstanceFile($dependencyIndex);
assert(file_exists($instanceFile), new Unbound($dependencyIndex));
/** @var object $instance */
$instance = require $instanceFile;
$this->singletons[$dependencyIndex] = $instance;

return $instance;
Expand All @@ -105,26 +137,21 @@ function (string $dependencyIndex, $injectionPoint = ['', '', '']) {
$this->functions = [$prototype, $singleton, $injectionPoint, $injector];
}

/**
* @return list<string>
*/
/** @return list<string> */
public function __sleep()
{
return ['scriptDir', 'singletons', 'lazyModule'];
}

public function __wakeup()
{
$this->__construct(
$this->scriptDir,
$this->lazyModule
);
$this->init($this->scriptDir, $this->lazyModule);
}

/**
* {@inheritdoc}
* {@inheritDoc}
*
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
* @SuppressWarnings(PHPMD.UnusedLocalVariable) // @phpstan-ignore-line
*/
public function getInstance($interface, $name = Name::ANY)
{
Expand All @@ -146,6 +173,7 @@ public function getInstance($interface, $name = Name::ANY)
/** @psalm-suppress UndefinedVariable */
$isSingleton = isset($isSingleton) && $isSingleton;
if ($isSingleton) {
/** @var object $instance */
$this->singletons[$dependencyIndex] = $instance;
}

Expand Down
6 changes: 3 additions & 3 deletions src/CompileNullObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

/**
* Convert NullObjectDependency to Dependency
*
* @psalm-import-type ScriptDir from CompileInjector
*/
final class CompileNullObject
{
/**
* @retrun void
*/
/** @param ScriptDir $scriptDir */
public function __invoke(Container $container, string $scriptDir): void
{
$container->map(static function (DependencyInterface $dependency) use ($scriptDir) {
Expand Down
4 changes: 1 addition & 3 deletions src/ContextInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

use function get_class;

/**
* @psalm-immutable
*/
/** @psalm-immutable */
final class ContextInjector
{
public static function getInstance(AbstractInjectorContext $injectorContext): InjectorInterface
Expand Down
Loading

0 comments on commit 2fbbb13

Please sign in to comment.