Skip to content

Commit

Permalink
Soothe SA
Browse files Browse the repository at this point in the history
  • Loading branch information
koriym committed Sep 7, 2023
1 parent 17b58a6 commit 75a3e8d
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 55 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"cs": ["./vendor/bin/phpcs --standard=./phpcs.xml src tests"],
"cs-fix": ["./vendor/bin/phpcbf src tests"],
"clean": ["./vendor/bin/phpstan clear-result-cache", "./vendor/bin/psalm --clear-cache", "rm -rf tests/tmp/*.php"],
"sa": ["./vendor/bin/psalm -c psalm.xml --show-info=true", "./vendor/bin/phpstan analyse -c phpstan.neon --no-progress "],
"sa": ["./vendor/bin/psalm -m -c psalm.xml --show-info=true", "./vendor/bin/phpstan analyse -c phpstan.neon --no-progress "],
"metrics": ["@test", "./vendor/bin/phpmetrics --report-html=build/metrics --exclude=Exception --log-junit=build/junit.xml --junit=build/junit.xml src"],
"phpmd": ["./vendor/bin/phpmd src/di text ./phpmd.xml"],
"build": ["@cs", "@sa", "@pcov", "@metrics"]
Expand Down
14 changes: 5 additions & 9 deletions src/di/AnnotatedClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,27 @@

namespace Ray\Di;

use Doctrine\Common\Annotations\Reader;
use Ray\Aop\ReflectionClass;
use Ray\Di\Di\PostConstruct;
use ReflectionClass;
use ReflectionMethod;

final class AnnotatedClass
{
/** @var Reader */
private $reader;

/** @var AnnotatedClassMethods */
private $injectionMethod;

public function __construct(Reader $reader)
public function __construct()
{
$this->reader = $reader;
$this->injectionMethod = new AnnotatedClassMethods($reader);
$this->injectionMethod = new AnnotatedClassMethods();
}

/**
* Return factory instance
*
* @phpstan-param ReflectionClass<object> $class Target class reflection

Check failure on line 24 in src/di/AnnotatedClass.php

View workflow job for this annotation

GitHub Actions / sa / Psalm

TooManyTemplateParams: Ray\Aop\ReflectionClass<object> has too many template params, expecting 0
*/

Check failure on line 25 in src/di/AnnotatedClass.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

There must be no blank lines after the function comment
public function getNewInstance(\Ray\Aop\ReflectionClass $class): NewInstance

public function getNewInstance(ReflectionClass $class): NewInstance

Check failure on line 27 in src/di/AnnotatedClass.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

PHPDoc tag @param for parameter $class contains generic type Ray\Aop\ReflectionClass<object> but class Ray\Aop\ReflectionClass is not generic.
{
$setterMethods = new SetterMethods([]);
$methods = $class->getMethods();
Expand Down
13 changes: 4 additions & 9 deletions src/di/AnnotatedClassMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Ray\Di;

use Doctrine\Common\Annotations\Reader;
use Ray\Aop\ReflectionClass;
use Ray\Aop\ReflectionMethod;
use Ray\Di\Di\InjectInterface;
Expand All @@ -14,16 +13,12 @@

final class AnnotatedClassMethods
{
/** @var Reader */
private $reader;

/** @var NameKeyVarString */
private $nameKeyVarString;

public function __construct(Reader $reader)
public function __construct()
{
$this->reader = $reader;
$this->nameKeyVarString = new NameKeyVarString($reader);
$this->nameKeyVarString = new NameKeyVarString();
}

/**
Expand All @@ -48,7 +43,7 @@ public function getConstructorName(ReflectionClass $class): Name
return new Name($named->value);
}

$name = ($this->nameKeyVarString)(new ReflectionMethod($class->name, $constructor->name));
$name = ($this->nameKeyVarString)(new ReflectionMethod($class->getName(), $constructor->getName()));
if ($name !== null) {
return new Name($name);
}
Expand All @@ -72,7 +67,7 @@ public function getSetterMethod(ReflectionMethod $method): ?SetterMethod
return $setterMethod;
}

private function getName(\Ray\Aop\ReflectionMethod $method): Name
private function getName(ReflectionMethod $method): Name
{
if (PHP_VERSION_ID >= 80000) {
$name = Name::withAttributes($method);
Expand Down
3 changes: 1 addition & 2 deletions src/di/Arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Ray\Di;

use Ray\Di\Exception\Unbound;
use Ray\ServiceLocator\ServiceLocator;
use ReflectionMethod;

final class Arguments
Expand Down Expand Up @@ -65,6 +64,6 @@ private function bindInjectionPoint(Container $container, Argument $argument): v
return;
}

(new Bind($container, InjectionPointInterface::class))->toInstance(new InjectionPoint($argument->get(), ServiceLocator::getReader()));
(new Bind($container, InjectionPointInterface::class))->toInstance(new InjectionPoint($argument->get()));
}
}
8 changes: 6 additions & 2 deletions src/di/Bind.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ public function to(string $class): self
/**
* Bind to constructor
*
* @param class-string $class class name
* @param class-string<T> $class class name
* @param array<string, string>|string $name "varName=bindName,..." or [$varName => $bindName, $varName => $bindName...]
*
* @throws ReflectionException
*
* @template T of object
*/
public function toConstructor(string $class, $name, ?InjectionPoints $injectionPoints = null, ?string $postConstruct = null): self
{
Expand All @@ -116,7 +118,9 @@ public function toConstructor(string $class, $name, ?InjectionPoints $injectionP

$this->untarget = null;
$postConstructRef = $postConstruct ? new ReflectionMethod($class, $postConstruct) : null;
$this->bound = (new DependencyFactory())->newToConstructor(new ReflectionClass($class), $name, $injectionPoints, $postConstructRef);
/** @var ReflectionClass<object> $reflection */
$reflection = new ReflectionClass($class);

Check failure on line 122 in src/di/Bind.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

PHPDoc tag @var for variable $reflection contains generic type Ray\Aop\ReflectionClass<object> but class Ray\Aop\ReflectionClass is not generic.

Check failure on line 122 in src/di/Bind.php

View workflow job for this annotation

GitHub Actions / sa / Psalm

TooManyTemplateParams: Ray\Aop\ReflectionClass<object> has too many template params, expecting 0
$this->bound = (new DependencyFactory())->newToConstructor($reflection, $name, $injectionPoints, $postConstructRef);
$this->container->add($this);

return $this;
Expand Down
3 changes: 2 additions & 1 deletion src/di/BindValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public function to(string $interface, string $class): ReflectionClass
*
* @phpstan-param class-string $provider
*
* @return ReflectionClass<object>
* @psalm-return ReflectionClass
* @phpstan-return ReflectionClass<object>
*
* @throws NotFound
*/
Expand Down
3 changes: 1 addition & 2 deletions src/di/DependencyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Ray\Di;

use Ray\Aop\ReflectionClass;
use Ray\ServiceLocator\ServiceLocator;
use ReflectionMethod;

use function assert;
Expand All @@ -20,7 +19,7 @@ final class DependencyFactory
*/
public function newAnnotatedDependency(ReflectionClass $class): Dependency

Check failure on line 20 in src/di/DependencyFactory.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

PHPDoc tag @param for parameter $class contains generic type Ray\Aop\ReflectionClass<object> but class Ray\Aop\ReflectionClass is not generic.
{
$annotateClass = new AnnotatedClass(ServiceLocator::getReader());
$annotateClass = new AnnotatedClass();
$newInstance = $annotateClass->getNewInstance($class);
$postConstruct = $annotateClass->getPostConstruct($class);

Expand Down
24 changes: 12 additions & 12 deletions src/di/InjectionPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Ray\Di;

use Doctrine\Common\Annotations\Reader;
use Ray\Aop\ReflectionClass;
use Ray\Aop\ReflectionMethod;
use Ray\Di\Di\Qualifier;
Expand All @@ -30,14 +29,13 @@ final class InjectionPoint implements InjectionPointInterface, Serializable
/** @var string */
private $pName;

public function __construct(ReflectionParameter $parameter, Reader $reader)
public function __construct(ReflectionParameter $parameter)
{
$this->parameter = $parameter;
$this->pFunction = (string) $parameter->getDeclaringFunction()->name;
$class = $parameter->getDeclaringClass();
$this->pClass = $class instanceof ReflectionClass ? $class->name : '';
$this->pName = $parameter->name;
$this->reader = $reader;
}

/**
Expand All @@ -56,9 +54,10 @@ public function getMethod(): ReflectionMethod
$this->parameter = $this->getParameter();
$class = $this->parameter->getDeclaringClass();
$method = $this->parameter->getDeclaringFunction()->getShortName();
assert(class_exists($class->name));
assert($class instanceof \ReflectionClass);
assert(class_exists($class->getName()));

return new ReflectionMethod($class->name, $method);
return new ReflectionMethod($class->getName(), $method);
}

/**
Expand All @@ -68,8 +67,9 @@ public function getClass(): ReflectionClass
{
$this->parameter = $this->getParameter();
$class = $this->parameter->getDeclaringClass();
assert($class instanceof \ReflectionClass);

return new ReflectionClass($class->name);
return new ReflectionClass($class->getName());
}

/**
Expand All @@ -90,21 +90,21 @@ public function getQualifiers(): array
}

/**
* @return array{0: Reader, 1: string, 2: string, 3: string}
* @return array<string>
*/
public function __serialize(): array
{
return [$this->reader, $this->pClass, $this->pFunction, $this->pName];
return [$this->pClass, $this->pFunction, $this->pName];
}

/**
* {@inheritDoc}
*
* @param array{0: Reader, 1: string, 2: string, 3: string} $array
* @param array<string> $array
*/
public function __unserialize(array $array): void
{
[$this->reader, $this->pClass, $this->pFunction, $this->pName] = $array;
[$this->pClass, $this->pFunction, $this->pName] = $array;
}

public function serialize(): ?string
Expand All @@ -119,8 +119,8 @@ public function serialize(): ?string
*/
public function unserialize($data): void
{
/** @var array{0: Reader, 1: string, 2: string, 3: string} $array */
$array = unserialize($data, ['allowed_classes' => [Reader::class]]);
/** @var array<string> $array */
$array = unserialize($data);
$this->__unserialize($array);
}
}
9 changes: 0 additions & 9 deletions src/di/NameKeyVarString.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Ray\Di;

use Doctrine\Common\Annotations\Reader;
use Ray\Aop\ReflectionClass;
use Ray\Aop\ReflectionMethod;
use Ray\Di\Di\Named;
Expand All @@ -16,14 +15,6 @@

final class NameKeyVarString
{
/** @var Reader */
private $reader;

public function __construct(Reader $reader)
{
$this->reader = $reader;
}

public function __invoke(ReflectionMethod $method): ?string
{
$keyVal = [];
Expand Down
7 changes: 5 additions & 2 deletions src/di/Untarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@

final class Untarget
{
/** @var ReflectionClass<object> */
/**
* @phpstan-var ReflectionClass<object>
* @psalm-var ReflectionClass
*/
private $class;

/** @var string */
private $scope = Scope::PROTOTYPE;

/**
* @phpstan-param class-string $class
* @param class-string $class
*/
public function __construct(string $class)
{
Expand Down
3 changes: 1 addition & 2 deletions tests/di/AnnotatedClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Ray\Di;

use Doctrine\Common\Annotations\AnnotationReader;
use LogicException;
use PHPUnit\Framework\TestCase;
use Ray\Aop\ReflectionClass;
Expand All @@ -17,7 +16,7 @@ class AnnotatedClassTest extends TestCase
protected function setUp(): void
{
parent::setUp();
$this->annotatedClass = new AnnotatedClass(new AnnotationReader());
$this->annotatedClass = new AnnotatedClass();
}

public function testInvoke(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/di/BindTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function testProviderContext(): void
$container = new Container();
(new Bind($container, ProviderInterface::class))->toProvider(FakeContextualProvider::class, 'context_string');
$instance = $container->getInstance(ProviderInterface::class, Name::ANY);
assert(property_exists($instance, 'context'));
assert(property_exists($instance, 'context')); // @phpstan-ignore-line
$this->assertSame('context_string', $instance->context);
}
}
3 changes: 1 addition & 2 deletions tests/di/InjectionPointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Ray\Di;

use Doctrine\Common\Annotations\AnnotationReader;
use PHPUnit\Framework\TestCase;
use ReflectionParameter;

Expand All @@ -19,7 +18,7 @@ class InjectionPointTest extends TestCase
protected function setUp(): void
{
$this->parameter = new ReflectionParameter([FakeWalkRobot::class, '__construct'], 'rightLeg');
$this->ip = new InjectionPoint($this->parameter, new AnnotationReader());
$this->ip = new InjectionPoint($this->parameter);
}

public function testGetParameter(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/di/InjectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ protected function configure()
$instance = $injector->getInstance(FakeAop::class);
$result = $instance->returnSame(2);
$this->assertSame(2, $result);
assert(isset($instance->bindings));
assert(property_exists($instance, 'bindings')); // @phpstan-ignore-line
assert(isset($instance->bindings['returnSame'][0]));
$this->assertInstanceOf(NullInterceptor::class, $instance->bindings['returnSame'][0]);
}
Expand Down

0 comments on commit 75a3e8d

Please sign in to comment.