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

Update PHP versions in CI workflow and update QA tools latest #114

Merged
merged 24 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1190088
Update PHP versions in CI workflow
koriym Nov 26, 2024
7d999b0
Remove redundant configuration files and update settings
koriym Nov 27, 2024
2e68149
Update dependencies in composer.lock file
koriym Nov 27, 2024
78fd641
Add dependency doctrine/cache ^1.0 check
koriym Nov 27, 2024
08d52d7
Soothe SA and fix typos an
koriym Nov 27, 2024
9acbc02
Improve validation in testDomainException
koriym Nov 27, 2024
280fc18
Soothe CS
koriym Nov 27, 2024
1f6b78b
Rename file
koriym Nov 27, 2024
e59a145
Update tests/Fake/FakeInvalidDependency.php
koriym Nov 27, 2024
4f94131
Update tests/ScriptInjectorTest.php
koriym Nov 27, 2024
9694596
Ignore PHPStan errors in DependencyCompilerTest
koriym Nov 27, 2024
7974c32
Update doctrine coding standard v12
koriym Nov 27, 2024
7e876a3
Update vimeo/psalm ^5
koriym Nov 27, 2024
273e02e
Suppress PHPStan false-positive in test assertions
koriym Nov 27, 2024
0223bea
fixup! Update doctrine coding standard v12
koriym Nov 27, 2024
6ef18b8
Refactor initialization and add file existence checks
koriym Nov 27, 2024
ba56a35
Update type hint for $ip variable and injection point
koriym Nov 27, 2024
bdfec2c
Refactor CompileInjector for better type safety
koriym Nov 27, 2024
ba4bd91
Add injection point and injector type annotations
koriym Nov 27, 2024
0165a59
Remove unnecessary unset statements in test files
koriym Nov 27, 2024
9aa4f76
fixup! Update PHP versions in CI workflow
koriym Nov 27, 2024
0d0db43
fixup! Remove unnecessary unset statements in test files
koriym Nov 27, 2024
d5c706c
Normalize line endings in test assertions for Windows platform
koriym Nov 27, 2024
df0fe4f
Refactor: ensure consistent line ending normalization
koriym Nov 27, 2024
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
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
koriym marked this conversation as resolved.
Show resolved Hide resolved
*/
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここ string が追加されて既存のものとシグネチャ変わってしまいますが、大丈夫ですかね ...?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finalクラスだしOKじゃないでしょうか?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NaokiTsuchiya ということでmergeします!

{
$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
Loading