Skip to content

Commit

Permalink
Soothe SA and fix typos an
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
koriym committed Nov 27, 2024
1 parent 78fd641 commit 08d52d7
Show file tree
Hide file tree
Showing 31 changed files with 162 additions and 85 deletions.
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
9 changes: 8 additions & 1 deletion src/AbstractInjectorContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions src/CachedInjectorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@

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 +25,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 +58,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 +72,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
2 changes: 1 addition & 1 deletion src/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
26 changes: 17 additions & 9 deletions src/CompileInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@
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;

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

Expand All @@ -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
*/
Expand All @@ -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;

Expand Down Expand Up @@ -124,7 +131,7 @@ public function __wakeup()
/**
* {@inheritdoc}
*
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
* @SuppressWarnings(PHPMD.UnusedLocalVariable) // @phpstan-ignore-line
*/
public function getInstance($interface, $name = Name::ANY)
{
Expand All @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion src/CompileNullObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
14 changes: 7 additions & 7 deletions src/DependencyCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/DependencySaver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 10 additions & 2 deletions src/DiCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/FunctionCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/GraphDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,28 @@

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;
$this->scriptDir = $scriptDir;
}

/**
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
* @SuppressWarnings(PHPMD.UnusedLocalVariable) // @phpstan-ignore-line
*/
public function __invoke(): void
{
Expand Down
9 changes: 8 additions & 1 deletion src/InjectionPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion src/InjectorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 08d52d7

Please sign in to comment.