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

Use Ray.Aop's _setBindings() and Bump SA tool #298

Merged
merged 22 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f490660
Add "baseline" script to composer.json
koriym Feb 22, 2025
8b88357
Add PHPStan and Psalm baselines for static analysis
koriym Feb 22, 2025
0e8c15f
Refactor AOP binding logic into a dedicated method
koriym Feb 22, 2025
fa7111b
Update Ray/AOP dependency to ^2.x-dev
koriym Feb 22, 2025
f8e3738
Refactor type assertions and annotations for clarity.
koriym Feb 22, 2025
559e01e
fixup! Add "baseline" script to composer.json
koriym Feb 22, 2025
47fc6f4
Update dependencies in composer.lock to latest versions
koriym Feb 22, 2025
63a90ea
fixup! Refactor type assertions and annotations for clarity.
koriym Feb 22, 2025
a9711b8
Update psalm command to disable info display
koriym Feb 22, 2025
4ad4d5f
Normalize line endings in ModuleTest string assertions
koriym Feb 22, 2025
5867f00
Update dependencies in tools/composer.json
koriym Feb 22, 2025
590f77b
fixup! Update dependencies in tools/composer.json
koriym Feb 22, 2025
005ad4d
Make classes final for better safety and immutability.
koriym Feb 22, 2025
6543ac7
Remove unnecessary test directory from psalm.xml configuration.
koriym Feb 22, 2025
2e18991
Soothe SA:vRefactor type checks and suppress annotations.
koriym Feb 22, 2025
ce7af2d
Add OS-specific requirement annotations to tests
koriym Feb 22, 2025
704eba1
Make classes final in DI modules
koriym Feb 23, 2025
ad2cd3a
Simplify function name assignment in InjectionPoint.
koriym Feb 23, 2025
9c6eeb4
Update psalm config and downgrade sebastian/diff package
koriym Feb 23, 2025
75924fd
Update Ray/AOP dependency to ^2.18
koriym Feb 23, 2025
3ca6587
Soothe phpstan: Fix type annotations and remove unnecessary suppressions
koriym Feb 23, 2025
e290e15
Remove #[Override] annotations from Argument.php
koriym Feb 23, 2025
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
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"koriym/attributes": "^1.0.4",
"koriym/null-object": "^1.0",
"koriym/param-reader": "^1.0",
"ray/aop": "^2.16",
"ray/aop": "^2.18",
"ray/compiler": "^1.10.3"
},
"require-dev": {
Expand Down Expand Up @@ -47,10 +47,11 @@
"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 -m -c psalm.xml --show-info=true", "phpstan analyse -c phpstan.neon --no-progress "],
"sa": ["psalm -m -c psalm.xml --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"],
"build": ["@cs", "@sa", "@pcov", "@metrics"]
"build": ["@cs", "@sa", "@pcov", "@metrics"],
"baseline": ["phpstan analyse -c phpstan.neon --generate-baseline", "psalm --set-baseline=psalm-baseline.xml"]
},
"extra": {
"bamarni-bin": {
Expand Down
7 changes: 7 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
ignoreErrors:
-
message: '#^Negated boolean expression is always false\.$#'
identifier: booleanNot.alwaysFalse
count: 1
path: src/di/AnnotatedClassMethods.php
33 changes: 33 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.30.0@d0bc6e25d89f649e4f36a534f330f8bb4643dd69">
<file src="src/di/AnnotatedClassMethods.php">
<DocblockTypeContradiction occurrences="2">
<code>! $constructor</code>
<code>$constructor</code>
</DocblockTypeContradiction>
</file>
<file src="src/di/AspectBind.php">
<MixedArrayOffset occurrences="1">
<code>$instantiatedBindings[$methodName]</code>
</MixedArrayOffset>
<MixedAssignment occurrences="3">
<code>$interceptorClassName</code>
<code>$interceptorClassNames</code>
<code>$methodName</code>
</MixedAssignment>
<MixedReturnTypeCoercion occurrences="2">
<code>$instantiatedBindings</code>
<code>array&lt;non-empty-string, list&lt;MethodInterceptor&gt;&gt;</code>
</MixedReturnTypeCoercion>
</file>
<file src="src/di/Dependency.php">
<InvalidArgument occurrences="1">
<code>$pointcuts</code>
</InvalidArgument>
</file>
<file src="src/di/NewInstance.php">
<InvalidArgument occurrences="1">
<code>$this-&gt;bind-&gt;inject($container)</code>
</InvalidArgument>
</file>
</files>
7 changes: 5 additions & 2 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config https://psalm.dev/schema/config"
findUnusedCode="false"
>
<projectFiles>
<directory name="src/di"/>
<directory name="tests/type"/>
</projectFiles>
<stubs>
<file name="tests/stub/BindInterface.phpstub"/>
</stubs>
<plugins><pluginClass class="Psalm\PhpUnitPlugin\Plugin"/></plugins></psalm>
<issueHandlers>
<MissingOverrideAttribute errorLevel="suppress"/>
</issueHandlers>
</psalm>
3 changes: 3 additions & 0 deletions src/di/AnnotatedClassMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Ray\Di\Di\InjectInterface;
use Ray\Di\Di\Named;

use function assert;

use const PHP_VERSION_ID;

final class AnnotatedClassMethods
Expand Down Expand Up @@ -38,6 +40,7 @@ public function getConstructorName(ReflectionClass $class): Name
}
}

assert($constructor instanceof ReflectionMethod);
$named = $constructor->getAnnotation(Named::class);
if ($named instanceof Named) {
return new Name($named->value);
Expand Down
2 changes: 1 addition & 1 deletion src/di/AspectBind.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(AopBind $bind)
/**
* Instantiate interceptors
*
* @return array<string, list<MethodInterceptor>>
* @return array<non-empty-string, list<MethodInterceptor>>
*/
public function inject(Container $container): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/di/AssistedInjectModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* Assisted module for php8 attributes
*/
class AssistedInjectModule extends AbstractModule
final class AssistedInjectModule extends AbstractModule
{
protected function configure(): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/di/AssistedModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use const PHP_VERSION_ID;

class AssistedModule extends AbstractModule
final class AssistedModule extends AbstractModule
{
protected function configure(): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/di/Bind.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function toConstructor(string $class, $name, ?InjectionPoints $injectionP
}

$this->untarget = null;
$postConstructRef = $postConstruct ? new ReflectionMethod($class, $postConstruct) : null;
$postConstructRef = $postConstruct !== null ? new ReflectionMethod($class, $postConstruct) : null;
/** @var ReflectionClass<object> $reflection */
$reflection = new ReflectionClass($class);
$this->bound = (new DependencyFactory())->newToConstructor($reflection, $name, $injectionPoints, $postConstructRef);
Expand Down
2 changes: 1 addition & 1 deletion src/di/BindValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function to(string $interface, string $class): ReflectionClass
throw new InvalidType("[{$class}] is no implemented [{$interface}] interface");
}

return new ReflectionClass($class); // @phpstan-ignore-line
return new ReflectionClass($class);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/di/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function getInstanceWithArgs(string $interface, array $params)
throw new BadMethodCallException($interface);
}

/** @psalm-suppress ArgumentTypeCoercion */
return $dependency->injectWithArgs($this, $params);
}

Expand Down
8 changes: 4 additions & 4 deletions src/di/Dependency.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ public function register(array &$container, Bind $bind): void
public function inject(Container $container)
{
// singleton ?
if ($this->isSingleton === true && $this->instance) {
if ($this->isSingleton === true && $this->instance !== null) {
return $this->instance;
}

// create dependency injected instance
$this->instance = ($this->newInstance)($container);

// @PostConstruct
if ($this->postConstruct) {
if ($this->postConstruct !== null) {
assert(method_exists($this->instance, $this->postConstruct));
$this->instance->{$this->postConstruct}();
}
Expand All @@ -91,15 +91,15 @@ public function inject(Container $container)
public function injectWithArgs(Container $container, array $params)
{
// singleton ?
if ($this->isSingleton === true && $this->instance) {
if ($this->isSingleton === true && $this->instance !== null) {
return $this->instance;
}

// create dependency injected instance
$this->instance = $this->newInstance->newInstanceArgs($container, $params);

// @PostConstruct
if ($this->postConstruct) {
if ($this->postConstruct !== null) {
assert(method_exists($this->instance, $this->postConstruct));
$this->instance->{$this->postConstruct}();
}
Expand Down
2 changes: 1 addition & 1 deletion src/di/DependencyProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function register(array &$container, Bind $bind): void
*/
public function inject(Container $container)
{
if ($this->isSingleton && $this->instance) {
if ($this->isSingleton && $this->instance !== null) {
return $this->instance;
}

Expand Down
2 changes: 1 addition & 1 deletion src/di/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

namespace Ray\Di;

class Exception extends \Exception
final class Exception extends \Exception
{
}
2 changes: 1 addition & 1 deletion src/di/Exception/DirectoryNotWritable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

use RuntimeException;

class DirectoryNotWritable extends RuntimeException implements ExceptionInterface
final class DirectoryNotWritable extends RuntimeException implements ExceptionInterface
{
}
2 changes: 1 addition & 1 deletion src/di/Exception/InvalidContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

use InvalidArgumentException;

class InvalidContext extends InvalidArgumentException implements ExceptionInterface
final class InvalidContext extends InvalidArgumentException implements ExceptionInterface
{
}
2 changes: 1 addition & 1 deletion src/di/Exception/InvalidProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

use InvalidArgumentException;

class InvalidProvider extends InvalidArgumentException implements ExceptionInterface
final class InvalidProvider extends InvalidArgumentException implements ExceptionInterface
{
}
2 changes: 1 addition & 1 deletion src/di/Exception/InvalidToConstructorNameParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
/**
* @see https://github.com/ray-di/Ray.Di#constructor-bindings
*/
class InvalidToConstructorNameParameter extends InvalidArgumentException implements ExceptionInterface
final class InvalidToConstructorNameParameter extends InvalidArgumentException implements ExceptionInterface
{
}
2 changes: 1 addition & 1 deletion src/di/Exception/InvalidType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

use InvalidArgumentException;

class InvalidType extends InvalidArgumentException implements ExceptionInterface
final class InvalidType extends InvalidArgumentException implements ExceptionInterface
{
}
2 changes: 1 addition & 1 deletion src/di/Exception/MethodInvocationNotAvailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

namespace Ray\Di\Exception;

class MethodInvocationNotAvailable extends Unbound
final class MethodInvocationNotAvailable extends Unbound
{
}
2 changes: 1 addition & 1 deletion src/di/Exception/NotFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

use LogicException;

class NotFound extends LogicException implements ExceptionInterface
final class NotFound extends LogicException implements ExceptionInterface
{
}
2 changes: 1 addition & 1 deletion src/di/Exception/SetNotFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

use LogicException;

class SetNotFound extends LogicException implements ExceptionInterface
final class SetNotFound extends LogicException implements ExceptionInterface
{
}
2 changes: 1 addition & 1 deletion src/di/Exception/Untargeted.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

namespace Ray\Di\Exception;

class Untargeted extends Unbound
final class Untargeted extends Unbound
{
}
2 changes: 1 addition & 1 deletion src/di/InjectionPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class InjectionPoint implements InjectionPointInterface, Serializable
public function __construct(ReflectionParameter $parameter)
{
$this->parameter = $parameter;
$this->pFunction = (string) $parameter->getDeclaringFunction()->name;
$this->pFunction = $parameter->getDeclaringFunction()->name;
$class = $parameter->getDeclaringClass();
$this->pClass = $class instanceof ReflectionClass ? $class->name : '';
$this->pName = $parameter->name;
Expand Down
2 changes: 1 addition & 1 deletion src/di/Injector.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use function str_replace;
use function sys_get_temp_dir;

class Injector implements InjectorInterface
final class Injector implements InjectorInterface
{
/** @var non-empty-string */
private $classDir;
Expand Down
2 changes: 1 addition & 1 deletion src/di/MultiBinding/MultiBindingModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Ray\Di\AbstractModule;
use Ray\Di\Scope;

class MultiBindingModule extends AbstractModule
final class MultiBindingModule extends AbstractModule
{
protected function configure(): void
{
Expand Down
4 changes: 1 addition & 3 deletions src/di/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use ReflectionMethod;
use ReflectionParameter;

use function assert;
use function class_exists;
use function explode;
use function get_class;
Expand Down Expand Up @@ -153,12 +152,11 @@ private function parseName(string $name): array
if (isset($exploded[1])) {
[$key, $value] = $exploded;
if (isset($key[0]) && $key[0] === '$') {
assert(is_string($key)); // @phpstan-ignore-line
$key = substr($key, 1);
}

/** @psalm-suppress MixedArgument */
$names[trim($key)] = trim($value);
$names[trim((string) $key)] = trim($value);
}
}

Expand Down
21 changes: 15 additions & 6 deletions src/di/NewInstance.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Ray\Di;

use Ray\Aop\Bind as AopBind;
use Ray\Aop\WeavedInterface;
use ReflectionClass;
use ReflectionException;

Expand Down Expand Up @@ -47,7 +48,7 @@ public function __construct(
*/
public function __invoke(Container $container): object
{
/** @psalm-suppress MixedMethodCall */
/** @psalm-suppress MixedMethodCall, ArgumentTypeCoercion */
$instance = $this->arguments instanceof Arguments ? (new ReflectionClass($this->class))->newInstanceArgs($this->arguments->inject($container)) : new $this->class();

return $this->postNewInstance($container, $instance);
Expand All @@ -68,6 +69,7 @@ public function __toString()
*/
public function newInstanceArgs(Container $container, array $params): object
{
/** @psalm-suppress ArgumentTypeCoercion */
$instance = (new ReflectionClass($this->class))->newInstanceArgs($params);

return $this->postNewInstance($container, $instance);
Expand All @@ -94,15 +96,22 @@ public function accept(VisitorInterface $visitor): void

private function postNewInstance(Container $container, object $instance): object
{
// bind dependency injected interceptors
if ($this->bind instanceof AspectBind) {
assert(isset($instance->bindings));
$instance->bindings = $this->bind->inject($container);
}
$this->enableAop($instance, $container);

// setter injection
($this->setterMethods)($instance, $container);

return $instance;
}

public function enableAop(object $instance, Container $container): void
{
if (! $this->bind instanceof AspectBind) {
return;
}

assert($instance instanceof WeavedInterface);

$instance->_setBindings($this->bind->inject($container)); // Ray.Aop ^2.18
}
}
2 changes: 1 addition & 1 deletion src/di/NullModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Ray\Di;

class NullModule extends AbstractModule
final class NullModule extends AbstractModule
{
protected function configure(): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/di/ProviderSetModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Ray\Di;

class ProviderSetModule extends AbstractModule
final class ProviderSetModule extends AbstractModule
{
protected function configure(): void
{
Expand Down
Loading
Loading