From 08d52d7c5843462e3fcad5ab003a70d63e770b0c Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Wed, 27 Nov 2024 10:10:21 +0900 Subject: [PATCH] Soothe SA and fix typos an Added the @psalm-import-type ScriptDir annotation and updated the type hints across multiple classes and methods for improved type safety. This involved specifying ScriptDir in parameter and property docblocks, as well as typedefs in several files. Additionally, unnecessary assert statements were removed to clean up the test cases. --- src-deprecated/ReaderProvider.php | 2 +- src/AbstractInjectorContext.php | 9 ++++++- src/CachedInjectorFactory.php | 7 +++++ src/Code.php | 2 +- src/CompileInjector.php | 26 ++++++++++++------- src/CompileNullObject.php | 4 ++- src/DependencyCode.php | 14 +++++----- src/DependencySaver.php | 8 +++++- src/DiCompiler.php | 12 +++++++-- src/FunctionCode.php | 2 +- src/GraphDumper.php | 10 +++++-- src/InjectionPoint.php | 9 ++++++- src/InjectorFactory.php | 9 ++++++- src/Normalizer.php | 3 ++- src/OnDemandCompiler.php | 22 +++++++++++----- src/ScriptInjector.php | 16 +++++++----- src/ScriptinjectorModule.php | 10 +++++-- tests/AssistedTest.php | 10 +++---- tests/CachedFactoryTest.php | 5 +++- tests/CompileInjectorTest.php | 8 ++---- tests/ContextInjectorTest.php | 7 +---- tests/DevCache.php | 2 +- tests/DiCompilerTest.php | 3 --- .../Assisted/FakeAssistedInjectConsumer.php | 2 +- tests/Fake/Deep/FakeScriptInjectorContext.php | 1 + tests/Fake/FakeCar3.php | 1 + tests/Fake/FakeInvalidDependency.php | 3 +++ tests/Fake/script/singleton.php | 2 +- tests/InjectorFactoryTest.php | 6 ++--- tests/MultiBindingTest.php | 15 ++++++++++- tests/ScriptInjectorTest.php | 17 +++--------- 31 files changed, 162 insertions(+), 85 deletions(-) diff --git a/src-deprecated/ReaderProvider.php b/src-deprecated/ReaderProvider.php index b441acac..bfbc6040 100644 --- a/src-deprecated/ReaderProvider.php +++ b/src-deprecated/ReaderProvider.php @@ -12,7 +12,7 @@ * @deprecated * @codeCoverageIgnore */ -final class ReaderProvider implements ProviderInterface +final class ReaderProvider implements ProviderInterface // @phpstan-ignore-line { public function get(): Reader { diff --git a/src/AbstractInjectorContext.php b/src/AbstractInjectorContext.php index a328d61e..78a268a1 100644 --- a/src/AbstractInjectorContext.php +++ b/src/AbstractInjectorContext.php @@ -6,15 +6,22 @@ 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; diff --git a/src/CachedInjectorFactory.php b/src/CachedInjectorFactory.php index d9fdc380..49f33ef4 100644 --- a/src/CachedInjectorFactory.php +++ b/src/CachedInjectorFactory.php @@ -6,6 +6,7 @@ use Doctrine\Common\Cache\CacheProvider; use Ray\Di\AbstractModule; +use Ray\Di\Annotation\ScriptDir; use Ray\Di\InjectorInterface; use Ray\Di\NullCache; @@ -13,6 +14,9 @@ use function serialize; use function unserialize; +/** + * @psalm-import-type ScriptDir from CompileInjector + */ final class CachedInjectorFactory { /** @var array */ @@ -21,6 +25,7 @@ final class CachedInjectorFactory /** * @param callable(): AbstractModule $modules * @param array $savedSingletons + * @param ScriptDir $scriptDir */ public static function getInstance(string $injectorId, string $scriptDir, callable $modules, ?CacheProvider $cache = null, array $savedSingletons = []): InjectorInterface { @@ -53,6 +58,7 @@ public static function getInstance(string $injectorId, string $scriptDir, callab /** * @param callable(): AbstractModule $modules * @param array $savedSingletons + * @param ScriptDir $scriptDir */ public static function getOverrideInstance( string $scriptDir, @@ -66,6 +72,7 @@ public static function getOverrideInstance( /** * @param callable(): AbstractModule $modules * @param array $savedSingletons + * @param ScriptDir $scriptDir */ private static function getInjector(callable $modules, string $scriptDir, array $savedSingletons, ?AbstractModule $module = null): InjectorInterface { diff --git a/src/Code.php b/src/Code.php index 46c4f677..0da7986b 100644 --- a/src/Code.php +++ b/src/Code.php @@ -19,7 +19,7 @@ final class Code private $node; /** - * @SuppressWarnings(PHPMD.BooleanArgumentFlag) + * @SuppressWarnings(PHPMD.BooleanArgumentFlag) // @phpstan-ignore-line */ public function __construct(Node $node, bool $isSingleton = false, ?IpQualifier $qualifier = null) { diff --git a/src/CompileInjector.php b/src/CompileInjector.php index bf328f75..bccc042d 100644 --- a/src/CompileInjector.php +++ b/src/CompileInjector.php @@ -18,12 +18,16 @@ use function str_replace; use function touch; +/** + * @psalm-type ScriptDir = non-empty-string + * @psalm-type Ip = array{0: string, 1: string, 2: string} + */ final class CompileInjector implements ScriptInjectorInterface { public const INSTANCE = '%s/%s.php'; public const COMPILE_CHECK = '%s/compiled'; - /** @var string */ + /** @var ScriptDir */ private $scriptDir; /** @@ -31,7 +35,7 @@ final class CompileInjector implements ScriptInjectorInterface * * [$class, $method, $parameter] * - * @var array{0: string, 1: string, 2: string} + * @var Ip */ private $ip = ['', '', '']; @@ -52,19 +56,21 @@ final class CompileInjector implements ScriptInjectorInterface 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) { - $this->scriptDir = rtrim($scriptDir, '/'); + /** @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 */ @@ -76,18 +82,19 @@ function (string $dependencyIndex, array $injectionPoint = ['', '', '']) { }; $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; + /** @var object $instance */ $instance = require $this->getInstanceFile($dependencyIndex); $this->singletons[$dependencyIndex] = $instance; @@ -124,7 +131,7 @@ public function __wakeup() /** * {@inheritdoc} * - * @SuppressWarnings(PHPMD.UnusedLocalVariable) + * @SuppressWarnings(PHPMD.UnusedLocalVariable) // @phpstan-ignore-line */ public function getInstance($interface, $name = Name::ANY) { @@ -146,6 +153,7 @@ public function getInstance($interface, $name = Name::ANY) /** @psalm-suppress UndefinedVariable */ $isSingleton = isset($isSingleton) && $isSingleton; if ($isSingleton) { + /** @var object $instance */ $this->singletons[$dependencyIndex] = $instance; } diff --git a/src/CompileNullObject.php b/src/CompileNullObject.php index c832b1a7..70685b0c 100644 --- a/src/CompileNullObject.php +++ b/src/CompileNullObject.php @@ -10,11 +10,13 @@ /** * 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 { diff --git a/src/DependencyCode.php b/src/DependencyCode.php index 1a236199..1b04abd9 100644 --- a/src/DependencyCode.php +++ b/src/DependencyCode.php @@ -88,9 +88,9 @@ public function setContext($context) $this->context = $context; } - public function setQaulifier(IpQualifier $qualifer): void + public function setQualifier(IpQualifier $qualifier): void { - $this->qualifier = $qualifer; + $this->qualifier = $qualifier; } public function getIsSingletonCode(bool $isSingleton): Expr\Assign @@ -123,10 +123,10 @@ private function getDependencyCode(Dependency $dependency): Code $node[] = $this->getIsSingletonCode($isSingleton); $node[] = new Node\Stmt\Return_(new Node\Expr\Variable('instance')); $namespace = $this->factory->namespace('Ray\Di\Compiler')->addStmts($node)->getNode(); - $qualifer = $this->qualifier; + $qualifier = $this->qualifier; $this->qualifier = null; - return new Code($namespace, $isSingleton, $qualifer); + return new Code($namespace, $isSingleton, $qualifier); } /** @@ -151,11 +151,11 @@ private function getProviderCode(DependencyProvider $provider): Code $node[] = $this->getIsSingletonCode($isSingleton); $node[] = new Stmt\Return_(new MethodCall(new Expr\Variable('instance'), 'get')); /** @psalm-suppress InvalidArgument */ - $node = $this->factory->namespace('Ray\Di\Compiler')->addStmts($node)->getNode(); - $qualifer = $this->qualifier; + $node = $this->factory->namespace('Ray\Di\Compiler')->addStmts($node)->getNode(); // @phpstan-ignore-line + $qualifier = $this->qualifier; $this->qualifier = null; - return new Code($node, $isSingleton, $qualifer); + return new Code($node, $isSingleton, $qualifier); } private function getSetContextCode(string $context): MethodCall diff --git a/src/DependencySaver.php b/src/DependencySaver.php index b2e1c6b1..3f0fc2fd 100644 --- a/src/DependencySaver.php +++ b/src/DependencySaver.php @@ -16,14 +16,20 @@ use const PHP_EOL; +/** + * @psalm-import-type ScriptDir from CompileInjector + */ final class DependencySaver { - /** @var string */ + /** @var ScriptDir */ private $scriptDir; /** @var FilePutContents */ private $filePutContents; + /** + * @param ScriptDir $scriptDir + */ public function __construct(string $scriptDir) { $this->scriptDir = $scriptDir; diff --git a/src/DiCompiler.php b/src/DiCompiler.php index 91745896..0605d8a7 100644 --- a/src/DiCompiler.php +++ b/src/DiCompiler.php @@ -25,9 +25,12 @@ use function sprintf; use function sys_get_temp_dir; +/** + * @psalm-import-type ScriptDir from CompileInjector + */ final class DiCompiler implements InjectorInterface { - /** @var string */ + /** @var ScriptDir */ private $scriptDir; /** @var Container */ @@ -45,9 +48,14 @@ final class DiCompiler implements InjectorInterface /** @var FilePutContents */ private $filePutContents; + /** + * @param ScriptDir $scriptDir + */ public function __construct(AbstractModule $module, string $scriptDir) { - $this->scriptDir = $scriptDir ?: sys_get_temp_dir(); + /** @var ScriptDir $scriptDir */ + $scriptDir = $scriptDir ?: sys_get_temp_dir(); + $this->scriptDir = $scriptDir; $this->container = $module->getContainer(); $this->dependencyCompiler = new DependencyCode($this->container); $this->module = $module; diff --git a/src/FunctionCode.php b/src/FunctionCode.php index 5291376a..5fa7a9a4 100644 --- a/src/FunctionCode.php +++ b/src/FunctionCode.php @@ -114,7 +114,7 @@ private function setQualifiers(ReflectionMethod $method, ReflectionParameter $pa Qualifier::class ); if ($qualifier instanceof Qualifier) { - $this->compiler->setQaulifier(new IpQualifier($param, $annotation)); + $this->compiler->setQualifier(new IpQualifier($param, $annotation)); } } } diff --git a/src/GraphDumper.php b/src/GraphDumper.php index 4a251389..26fbe7c8 100644 --- a/src/GraphDumper.php +++ b/src/GraphDumper.php @@ -19,14 +19,20 @@ use const LOCK_EX; +/** + * @psalm-import-type ScriptDir from CompileInjector + */ final class GraphDumper { /** @var Container */ private $container; - /** @var string */ + /** @var ScriptDir */ private $scriptDir; + /** + * @param ScriptDir $scriptDir + */ public function __construct(Container $container, string $scriptDir) { $this->container = $container; @@ -34,7 +40,7 @@ public function __construct(Container $container, string $scriptDir) } /** - * @SuppressWarnings(PHPMD.UnusedLocalVariable) + * @SuppressWarnings(PHPMD.UnusedLocalVariable) // @phpstan-ignore-line */ public function __invoke(): void { diff --git a/src/InjectionPoint.php b/src/InjectionPoint.php index 2ec30902..6841bc4e 100644 --- a/src/InjectionPoint.php +++ b/src/InjectionPoint.php @@ -6,6 +6,7 @@ use Ray\Aop\ReflectionClass; use Ray\Aop\ReflectionMethod; +use Ray\Di\Annotation\ScriptDir; use Ray\Di\InjectionPointInterface; use ReflectionParameter; use RuntimeException; @@ -19,14 +20,20 @@ use function str_replace; use function unserialize; +/** + * @psalm-import-type ScriptDir from CompileInjector + */ final class InjectionPoint implements InjectionPointInterface { /** @var ReflectionParameter */ private $parameter; - /** @var string */ + /** @var ScriptDir */ private $scriptDir; + /** + * @param ScriptDir $scriptDir + */ public function __construct(ReflectionParameter $parameter, string $scriptDir) { $this->parameter = $parameter; diff --git a/src/InjectorFactory.php b/src/InjectorFactory.php index 07481edd..326ee58f 100644 --- a/src/InjectorFactory.php +++ b/src/InjectorFactory.php @@ -6,6 +6,7 @@ use Ray\Compiler\Annotation\Compile; use Ray\Di\AbstractModule; +use Ray\Di\Annotation\ScriptDir; use Ray\Di\Exception\Unbound; use Ray\Di\Injector as RayInjector; use Ray\Di\InjectorInterface; @@ -15,11 +16,13 @@ /** * @psalm-immutable + * @psalm-import-type ScriptDir from CompileInjector */ final class InjectorFactory { /** * @param callable(): AbstractModule $modules + * @param ScriptDir $scriptDir */ public static function getInstance(callable $modules, string $scriptDir): InjectorInterface { @@ -43,13 +46,17 @@ public static function getInstance(callable $modules, string $scriptDir): Inject return self::getScriptInjector($scriptDir, $module); } + /** + * @param ScriptDir $scriptDir + */ private static function getScriptInjector(string $scriptDir, AbstractModule $module): ScriptInjector { return new ScriptInjector($scriptDir, static function () use ($scriptDir, $module) { - return new ScriptinjectorModule($scriptDir, $module); + return new ScriptInjectorModule($scriptDir, $module); }); } + /** @param ScriptDir $scriptDIr */ private static function getCompileInjector(string $scriptDIr, LazyModuleInterface $module): CompileInjector { return new CompileInjector($scriptDIr, $module); diff --git a/src/Normalizer.php b/src/Normalizer.php index 9491c290..a463c2e2 100644 --- a/src/Normalizer.php +++ b/src/Normalizer.php @@ -76,13 +76,14 @@ private function getValueNode($value): Expr /** * Return array or object node * - * @param array|mixed|object $value + * @param array|mixed|object $value * * @return Expr\Array_|Expr\FuncCall */ private function getValueNodeNonAtomic($value): Expr { if (is_array($value)) { + /** @var array $value */ return $this->arrayValue($value); } diff --git a/src/OnDemandCompiler.php b/src/OnDemandCompiler.php index 392afa5c..b664b000 100644 --- a/src/OnDemandCompiler.php +++ b/src/OnDemandCompiler.php @@ -8,6 +8,7 @@ use Ray\Aop\Pointcut; use Ray\Compiler\Exception\Unbound; use Ray\Di\AbstractModule; +use Ray\Di\Annotation\ScriptDir; use Ray\Di\Bind; use Ray\Di\Dependency; use Ray\Di\Exception\NotFound; @@ -17,15 +18,18 @@ use function explode; use function file_exists; use function file_get_contents; -use function is_array; use function is_bool; use function unserialize; use const E_NOTICE; +/** + * @psalm-import-type ScriptDir from CompileInjector + * @psalm-type Pointcuts = list + */ final class OnDemandCompiler { - /** @var string */ + /** @var ScriptDir */ private $scriptDir; /** @var ScriptInjector */ @@ -37,6 +41,9 @@ final class OnDemandCompiler /** @var CompileNullObject */ private $compiler; + /** + * @param ScriptDir $scriptDir + */ public function __construct(ScriptInjector $injector, string $scriptDir, AbstractModule $module) { $this->scriptDir = $scriptDir; @@ -65,8 +72,9 @@ public function __invoke(string $dependencyIndex): void ($this->compiler)($containerObject, $this->scriptDir); $dependency = $containerArray[$dependencyIndex]; + /** @var Pointcuts $pointCuts */ $pointCuts = $this->loadPointcuts(); - $isWeaverable = $dependency instanceof Dependency && is_array($pointCuts); + $isWeaverable = $dependency instanceof Dependency && ! empty($pointCuts); if ($isWeaverable) { $dependency->weaveAspects(new Compiler($this->scriptDir), $pointCuts); } @@ -76,19 +84,19 @@ public function __invoke(string $dependencyIndex): void } /** - * @return array|false + * @return Pointcuts */ - private function loadPointcuts() + private function loadPointcuts(): array { $pointcutsPath = $this->scriptDir . ScriptInjector::AOP; if (! file_exists($pointcutsPath)) { - return false; // @codeCoverageIgnore + return []; // @codeCoverageIgnore } $serialized = file_get_contents($pointcutsPath); assert(! is_bool($serialized)); $er = error_reporting(error_reporting() ^ E_NOTICE); - /** @var array $pointcuts */ + /** @var Pointcuts $pointcuts */ $pointcuts = unserialize($serialized, ['allowed_classes' => true]); error_reporting($er); diff --git a/src/ScriptInjector.php b/src/ScriptInjector.php index 21c7a37e..9980193e 100644 --- a/src/ScriptInjector.php +++ b/src/ScriptInjector.php @@ -38,6 +38,10 @@ use const DIRECTORY_SEPARATOR; use const E_NOTICE; +/** + * @psalm-import-type ScriptDir from CompileInjector + * @psalm-import-type Ip from CompileInjector + */ final class ScriptInjector implements ScriptInjectorInterface { public const MODULE = '/_module.txt'; @@ -46,9 +50,9 @@ final class ScriptInjector implements ScriptInjectorInterface public const INSTANCE = '%s/%s.php'; - public const QUALIFIER = '%s/qualifer/%s-%s-%s'; + public const QUALIFIER = '%s/qualifier/%s-%s-%s'; - /** @var string */ + /** @var non-empty-string */ private $scriptDir; /** @@ -86,8 +90,8 @@ final class ScriptInjector implements ScriptInjectorInterface private static $scriptDirs = []; /** - * @param string $scriptDir generated instance script folder path - * @param callable $lazyModule callable variable which return AbstractModule instance + * @param ScriptDir $scriptDir generated instance script folder path + * @param callable $lazyModule callable variable which return AbstractModule instance * * @psalm-suppress UnresolvableInclude */ @@ -112,7 +116,7 @@ function (string $dependencyIndex, array $injectionPoint = ['', '', '']) { }; $singleton = /** - * @param array{0: string, 1: string, 2: string} $injectionPoint + * @param Ip $injectionPoint * * @return mixed */ @@ -164,7 +168,7 @@ function () { /** * {@inheritdoc} * - * @SuppressWarnings(PHPMD.UnusedLocalVariable) + * @SuppressWarnings(PHPMD.UnusedLocalVariable) // @phpstan-ignore-line */ public function getInstance($interface, $name = Name::ANY) { diff --git a/src/ScriptinjectorModule.php b/src/ScriptinjectorModule.php index 1008c5a1..daa510cd 100644 --- a/src/ScriptinjectorModule.php +++ b/src/ScriptinjectorModule.php @@ -7,11 +7,17 @@ use Ray\Di\AbstractModule; use Ray\Di\InjectorInterface; -class ScriptinjectorModule extends AbstractModule +/** + * @psalm-import-type ScriptDir from CompileInjector + */ +class ScriptInjectorModule extends AbstractModule { - /** @var string */ + /** @var ScriptDir */ private $scriptDir; + /** + * @param ScriptDir $scriptDir + */ public function __construct(string $scriptDir, ?AbstractModule $module = null) { $this->scriptDir = $scriptDir; diff --git a/tests/AssistedTest.php b/tests/AssistedTest.php index fb5cc29c..da0d41d3 100644 --- a/tests/AssistedTest.php +++ b/tests/AssistedTest.php @@ -37,8 +37,8 @@ public function testAssisted(): void $consumer = $this->injector->getInstance(FakeAssistedConsumer::class); /** @var FakeAssistedConsumer $consumer */ $assistedDependency = $consumer->assistOne('a', 'b'); - $expecetd = FakeRobot::class; - $this->assertInstanceOf($expecetd, $assistedDependency); + $expected = FakeRobot::class; + $this->assertInstanceOf($expected, $assistedDependency); } public function testAssistedWithName(): void @@ -47,8 +47,8 @@ public function testAssistedWithName(): void $consumer = $this->injector->getInstance(FakeAssistedConsumer::class); /** @var FakeAssistedConsumer $consumer */ $assistedDependency = $consumer->assistWithName('a7'); - $expecetd = 1; - $this->assertSame($expecetd, $assistedDependency); + $expected = 1; + $this->assertSame($expected, $assistedDependency); } public function testAssistedAnyWithName(): void @@ -80,7 +80,7 @@ public function testAssistedMethodInvocationNotAvailable(): void $assistedDbProvider->get(); } - public function testAssistedCustomeInject(): void + public function testAssistedCustomInject(): void { $assistedConsumer = (new Injector(new FakeAssistedDbModule(), __DIR__ . '/tmp'))->getInstance(FakeAssistedParamsConsumer::class); /** @var FakeAssistedParamsConsumer $assistedConsumer */ diff --git a/tests/CachedFactoryTest.php b/tests/CachedFactoryTest.php index fdc1bc34..fcb2a553 100644 --- a/tests/CachedFactoryTest.php +++ b/tests/CachedFactoryTest.php @@ -11,6 +11,9 @@ use function spl_object_hash; +/** + * @psalm-type Context = 'dev'|'prod' + */ class CachedFactoryTest extends TestCase { public function testInstanceCachedInStaticMemory(): void @@ -31,7 +34,7 @@ public function testInstanceCachedInFileCache(): void } /** - * @param 'dev'|'prod' $context + * @param Context $context */ private function getInjector(string $context): InjectorInterface { diff --git a/tests/CompileInjectorTest.php b/tests/CompileInjectorTest.php index 813bf686..fa040a25 100644 --- a/tests/CompileInjectorTest.php +++ b/tests/CompileInjectorTest.php @@ -7,9 +7,7 @@ use PHPUnit\Framework\TestCase; use Ray\Di\Exception\Unbound; -use function assert; use function file_get_contents; -use function is_object; use function serialize; use function spl_object_hash; use function unserialize; @@ -54,7 +52,7 @@ public function testGetInstance(): void $this->assertInstanceOf(FakeCarInterface::class, $instance); } - public function testInjectopnPoint(): void + public function testInjectionPoint(): void { $instance = $this->injector->getInstance(FakeLoggerConsumer::class); $this->assertInstanceOf(FakeLoggerConsumer::class, $instance); @@ -64,8 +62,6 @@ public function testSingleton(): void { $instance1 = $this->injector->getInstance(FakeRobotInterface::class); $instance2 = $this->injector->getInstance(FakeRobotInterface::class); - assert(is_object($instance1)); - assert(is_object($instance2)); $this->assertSame(spl_object_hash($instance1), spl_object_hash($instance2)); } @@ -98,7 +94,7 @@ public function testUnboundCompileLogFile(): void /** * @depends testUnboundCompileLogFile */ - public function testCompileFaillureLog(): void + public function testCompileFailureLog(): void { $logFile = __DIR__ . '/tmp/_compile.log'; $this->assertFileExists(__DIR__ . '/tmp/compiled'); diff --git a/tests/ContextInjectorTest.php b/tests/ContextInjectorTest.php index 0a6a35ab..7c8fdb4b 100644 --- a/tests/ContextInjectorTest.php +++ b/tests/ContextInjectorTest.php @@ -13,8 +13,6 @@ use Ray\Di\Injector; use Ray\Di\InjectorInterface; -use function assert; - class ContextInjectorTest extends TestCase { public function testGetRayInjector(): InjectorInterface @@ -53,14 +51,11 @@ public function testContainerIsResetWhenTheInjectorIsRetrieved(AbstractInjectorC { $injector = ContextInjector::getInstance($context); $deep = $injector->getInstance(FakeDeep::class); - assert($deep instanceof FakeDeep); $deep->dep->changed = true; - $deep1 = $injector->getInstance(FakeDeep::class); - assert($deep1 instanceof FakeDeep); + $injector->getInstance(FakeDeep::class); $this->assertTrue($deep->dep->changed); $injector = ContextInjector::getInstance($context); $deep2 = $injector->getInstance(FakeDeep::class); - assert($deep2 instanceof FakeDeep); $this->assertFalse($deep2->dep->changed); $demand = $injector->getInstance(FakeDemand::class); $this->assertInstanceOf(FakeDemand::class, $demand); diff --git a/tests/DevCache.php b/tests/DevCache.php index 5414090c..fd281dc4 100644 --- a/tests/DevCache.php +++ b/tests/DevCache.php @@ -70,7 +70,7 @@ public function doDelete($id) */ public function doGetStats() { - return $this->cache->getStats(); // @phpstan-ignore-line + return $this->cache->getStats(); } /** diff --git a/tests/DiCompilerTest.php b/tests/DiCompilerTest.php index 75c3d803..2f4b57e4 100644 --- a/tests/DiCompilerTest.php +++ b/tests/DiCompilerTest.php @@ -13,7 +13,6 @@ use ReflectionParameter; use function assert; -use function property_exists; class DiCompilerTest extends TestCase { @@ -98,7 +97,6 @@ public function testInjectionPoint(): void $injector = new ScriptInjector(__DIR__ . '/tmp'); /** @var FakeLoggerConsumer $loggerConsumer */ $loggerConsumer = $injector->getInstance(FakeLoggerConsumer::class); - assert(property_exists($loggerConsumer, 'logger')); assert($loggerConsumer->logger instanceof FakeLogger); $this->assertSame(FakeLoggerConsumer::class, $loggerConsumer->logger->name); $this->assertSame('MEMORY', $loggerConsumer->logger->type); @@ -108,7 +106,6 @@ public function testInjectionPoint(): void $this->assertInstanceOf(ReflectionMethod::class, $ip->getMethod()); $this->assertSame('setLogger', $ip->getMethod()->name); $method = $ip->getMethod(); - assert($method instanceof ReflectionMethod); $this->assertInstanceOf(FakeLoggerInject::class, $method->getAnnotations()[0]); $this->assertInstanceOf(ReflectionParameter::class, $ip->getParameter()); $this->assertSame('logger', $ip->getParameter()->name); diff --git a/tests/Fake/Assisted/FakeAssistedInjectConsumer.php b/tests/Fake/Assisted/FakeAssistedInjectConsumer.php index 7de0d54e..19c05cf1 100644 --- a/tests/Fake/Assisted/FakeAssistedInjectConsumer.php +++ b/tests/Fake/Assisted/FakeAssistedInjectConsumer.php @@ -34,7 +34,7 @@ public function assistAny(#[Assisted, Named('one')] $var2 = null, #[Inject] ?Fak return [$var2, $robot]; } - public function assistCustomeAssistedInject(#[FakeInjectOne] int $one = 0): int + public function assistCustomAssistedInject(#[FakeInjectOne] int $one = 0): int { return $one; } diff --git a/tests/Fake/Deep/FakeScriptInjectorContext.php b/tests/Fake/Deep/FakeScriptInjectorContext.php index be54a321..80c1bc24 100644 --- a/tests/Fake/Deep/FakeScriptInjectorContext.php +++ b/tests/Fake/Deep/FakeScriptInjectorContext.php @@ -25,6 +25,7 @@ protected function doFetch($id) protected function doContains($id) { + unset($id); } protected function doSave($id, $data, $lifeTime = 0) diff --git a/tests/Fake/FakeCar3.php b/tests/Fake/FakeCar3.php index 05f71901..3f71a5e6 100644 --- a/tests/Fake/FakeCar3.php +++ b/tests/Fake/FakeCar3.php @@ -10,5 +10,6 @@ class FakeCar3 { public function __construct(DateTimeImmutable $dateTime) { + unset($dateTime); } } diff --git a/tests/Fake/FakeInvalidDependency.php b/tests/Fake/FakeInvalidDependency.php index 8c3fbc1c..071c6e37 100644 --- a/tests/Fake/FakeInvalidDependency.php +++ b/tests/Fake/FakeInvalidDependency.php @@ -12,14 +12,17 @@ class FakeInvalidDependency implements DependencyInterface { public function inject(Container $container) { + unset($container); } public function register(array &$container, Bind $bind) { + unset($container, $bind); } public function setScope($scope) { + unset($scope) } public function __toString() diff --git a/tests/Fake/script/singleton.php b/tests/Fake/script/singleton.php index e026bfac..9b1139bf 100644 --- a/tests/Fake/script/singleton.php +++ b/tests/Fake/script/singleton.php @@ -2,4 +2,4 @@ declare(strict_types=1); -$singleton('FakeInferface-*'); +$singleton('FakeInterface-*'); diff --git a/tests/InjectorFactoryTest.php b/tests/InjectorFactoryTest.php index 7e709bb1..e93a8799 100644 --- a/tests/InjectorFactoryTest.php +++ b/tests/InjectorFactoryTest.php @@ -27,10 +27,10 @@ public function getInstanceScriptInjector(): void { $injector = InjectorFactory::getInstance( static function (): AbstractModule { - $modue = new FakeToBindPrototypeModule(); - $modue->install(new FakeProdModule()); + $module = new FakeToBindPrototypeModule(); + $module->install(new FakeProdModule()); - return $modue; + return $module; }, __DIR__ . '/tmp/base' ); diff --git a/tests/MultiBindingTest.php b/tests/MultiBindingTest.php index 4236f9e7..ee10c203 100644 --- a/tests/MultiBindingTest.php +++ b/tests/MultiBindingTest.php @@ -42,6 +42,9 @@ protected function setUp(): void }); } + /** + * @return Map + */ public function testInjectMap(): Map { /** @var FakeMultiBindingConsumer $consumer */ @@ -52,6 +55,8 @@ public function testInjectMap(): Map } /** + * @param Map $map + * * @depends testInjectMap */ public function testMapInstance(Map $map): void @@ -61,6 +66,8 @@ public function testMapInstance(Map $map): void } /** + * @param Map $map + * * @depends testInjectMap */ public function testMapIteration(Map $map): void @@ -71,6 +78,8 @@ public function testMapIteration(Map $map): void } /** + * @param Map $map + * * @depends testInjectMap */ public function testIsSet(Map $map): void @@ -80,6 +89,8 @@ public function testIsSet(Map $map): void } /** + * @param Map $map + * * @depends testInjectMap */ public function testOffsetSet(Map $map): void @@ -89,6 +100,8 @@ public function testOffsetSet(Map $map): void } /** + * @param Map $map + * * @depends testInjectMap */ public function testOffsetUnset(Map $map): void @@ -106,7 +119,7 @@ public function testAnotherBinder(): void $this->assertSame(3, count($consumer->robots)); } - public function testMultipileModule(): void + public function testMultipleModule(): void { $module = new NullModule(); $binder = MultiBinder::newInstance($module, FakeEngineInterface::class); diff --git a/tests/ScriptInjectorTest.php b/tests/ScriptInjectorTest.php index db037373..c5953178 100644 --- a/tests/ScriptInjectorTest.php +++ b/tests/ScriptInjectorTest.php @@ -13,7 +13,6 @@ use function assert; use function count; use function glob; -use function property_exists; use function serialize; use function spl_object_hash; use function unserialize; @@ -122,7 +121,6 @@ public function testAop(): void $instance1 = $injector->getInstance(FakeCarInterface::class); $instance2 = $injector->getInstance(FakeCar::class); $instance3 = $injector->getInstance(FakeCar2::class); - assert($instance3 instanceof FakeCar2); $this->assertInstanceOf(WeavedInterface::class, $instance1); $this->assertInstanceOf(WeavedInterface::class, $instance2); $this->assertInstanceOf(WeavedInterface::class, $instance3); @@ -133,9 +131,7 @@ public function testOnDemandSingleton(): void { (new DiCompiler(new FakeToBindSingletonModule(), __DIR__ . '/tmp'))->compile(); $dependSingleton1 = $this->injector->getInstance(FakeDependSingleton::class); - assert($dependSingleton1 instanceof FakeDependSingleton); $dependSingleton2 = $this->injector->getInstance(FakeDependSingleton::class); - assert($dependSingleton2 instanceof FakeDependSingleton); $hash1 = spl_object_hash($dependSingleton1->robot); $hash2 = spl_object_hash($dependSingleton2->robot); $this->assertSame($hash1, $hash2); @@ -145,9 +141,7 @@ public function testOnDemandPrototype(): void { (new DiCompiler(new FakeCarModule(), __DIR__ . '/tmp'))->compile(); $fakeDependPrototype1 = $this->injector->getInstance(FakeDependPrototype::class); - assert($fakeDependPrototype1 instanceof FakeDependPrototype); $fakeDependPrototype2 = $this->injector->getInstance(FakeDependPrototype::class); - assert($fakeDependPrototype2 instanceof FakeDependPrototype); $hash1 = spl_object_hash($fakeDependPrototype1->car); $hash2 = spl_object_hash($fakeDependPrototype2->car); $this->assertNotSame($hash1, $hash2); @@ -156,7 +150,6 @@ public function testOnDemandPrototype(): void public function testOptional(): void { $optional = $this->injector->getInstance(FakeOptional::class); - assert($optional instanceof FakeOptional); $this->assertNull($optional->robot); } @@ -166,7 +159,6 @@ public function testDependInjector(): void $diCompiler->compile(); /** @var FakeFactory $factory */ $factory = $diCompiler->getInstance(FakeFactory::class); - assert(property_exists($factory, 'injector')); $this->assertInstanceOf(InjectorInterface::class, $factory->injector); $injector = new ScriptInjector(__DIR__ . '/tmp'); /** @var FakeFactory $factory */ @@ -177,9 +169,9 @@ public function testDependInjector(): void public function testUnbound(): void { $this->expectException(Unbound::class); - $this->expectExceptionMessage('NOCLASS-NONAME'); + $this->expectExceptionMessage('NO-CLASS-NO-NAME'); $injector = new ScriptInjector(__DIR__ . '/tmp'); - $injector->getInstance('NOCLASS', 'NONAME'); // @phpstan-ignore-line + $injector->getInstance('NO-CLASS', 'NO-NAME'); // @phpstan-ignore-line } public function testCompileOnDemand(): void @@ -191,7 +183,7 @@ static function () { } ); $car = $injector->getInstance(FakeCar::class); - $this->assertTrue($car instanceof FakeCar); + $this->assertTrue($car instanceof FakeCar); // @phpstan-ignore-line } public function testCompileOnDemandAop(): void @@ -203,7 +195,6 @@ static function () { } ); $aop = $injector->getInstance(FakeAopInterface::class); - assert($aop instanceof FakeAopInterface); $result = $aop->returnSame(1); $this->assertSame(2, $result); } @@ -219,7 +210,7 @@ static function () { $injector = unserialize($serialize); assert($injector instanceof InjectorInterface); $car = $injector->getInstance(FakeCar::class); - $this->assertTrue($car instanceof FakeCar); + $this->assertTrue($car instanceof FakeCar); // @phpstan-ignore-line } public function testCompileOnDemandAopSerialize(): void