Skip to content

Commit

Permalink
Merge pull request #178 from koriym/serializable
Browse files Browse the repository at this point in the history
Use service location to get annotation reader in AnnotatedWithMatcher
  • Loading branch information
koriym authored Jul 17, 2021
2 parents 1390df2 + 5f773a8 commit 1e92a1f
Show file tree
Hide file tree
Showing 13 changed files with 446 additions and 336 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ jobs:

- name: Install lowest dependencies
if: ${{ matrix.dependencies == 'lowest' }}
run: composer update --prefer-lowest --no-interaction --no-progress --no-suggest
run: composer update --prefer-lowest --no-interaction --no-progress

- name: Install highest dependencies
if: ${{ matrix.dependencies == 'highest' }}
run: composer update --no-interaction --no-progress --no-suggest
run: composer update --no-interaction --no-progress

- name: Run test suite
run: ./vendor/bin/phpunit --coverage-clover=coverage.xml
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/composer.lock
/coverage.xml
/vendor-bin/**/vendor
/.phpunit-cache
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": "^7.3 || ^8.0",
"doctrine/annotations": "^1.11",
"doctrine/annotations": "^1.12",
"koriym/attributes": "^1.0",
"nikic/php-parser": "^4.2"
},
Expand Down Expand Up @@ -43,7 +43,7 @@
"post-update-cmd": ["@composer bin all update --ansi"],
"test": ["./vendor/bin/phpunit"],
"tests": ["@cs", "@test", "@sa"],
"coverage": ["php -dzend_extension=xdebug.so ./vendor/bin/phpunit --coverage-text --coverage-html=build/coverage"],
"coverage": ["php -dzend_extension=xdebug.so -dxdebug.mode=coverage ./vendor/bin/phpunit --coverage-text --coverage-html=build/coverage"],
"pcov": ["php -dextension=pcov.so -d pcov.enabled=1 ./vendor/bin/phpunit --coverage-text --coverage-html=build/coverage --coverage-clover=coverage.xml"],
"cs": ["phpcs --standard=./phpcs.xml src tests"],
"cs-fix": ["./vendor/bin/phpcbf src tests"],
Expand Down
16 changes: 14 additions & 2 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<?xml version="1.0"?>
<ruleset
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="bearcs"
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Ray.Aop coding standard"
xsi:noNamespaceSchemaLocation="./vendor-bin/tools/vendor/squizlabs/php_codesniffer/phpcs.xsd">

<arg name="basepath" value="."/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="80"/>
<arg name="cache" value=".phpcs-cache"/>
<arg name="colors"/>

<!-- Compatibility with PHP 7.3.0 -->
<config name="php_version" value="70300"/>
com
<!-- Ignore warnings, show progress of the run and show sniff names -->
<arg value="nps"/>

Expand Down Expand Up @@ -51,6 +54,15 @@
</property>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Commenting.DocCommentSpacing">
<properties>
<property name="annotationsGroups" type="array">
<element value="@param, @psalm-param, @phpstan-param"/>
<element value="@return, @psalm-return, @phpstan-return"/>
<element value="@throws"/>
</property>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint">
<include-pattern>src/Module/*</include-pattern>
<include-pattern>tests/*</include-pattern>
Expand Down
7 changes: 5 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="tests/bootstrap.php">
<coverage>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="tests/bootstrap.php">
<coverage processUncoveredFiles="true" cacheDirectory="./.phpunit-cache">
<include>
<directory suffix=".php">src</directory>
</include>
Expand Down
18 changes: 2 additions & 16 deletions src/Matcher/AnnotatedWithMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,21 @@

namespace Ray\Aop\Matcher;

use Doctrine\Common\Annotations\AnnotationException;
use Doctrine\Common\Annotations\Reader;
use Ray\Aop\AbstractMatcher;
use Ray\ServiceLocator\ServiceLocator;
use ReflectionClass;
use ReflectionMethod;

final class AnnotatedWithMatcher extends AbstractMatcher
{
/** @var Reader */
private $reader;

/**
* @throws AnnotationException
*/
public function __construct()
{
parent::__construct();
$this->reader = ServiceLocator::getReader();
}

/**
* {@inheritdoc}
*/
public function matchesClass(ReflectionClass $class, array $arguments): bool
{
/** @var array<class-string> $arguments */
[$annotation] = $arguments;
$annotation = $this->reader->getClassAnnotation($class, $annotation);
$annotation = ServiceLocator::getReader()->getClassAnnotation($class, $annotation);

return (bool) $annotation;
}
Expand All @@ -44,7 +30,7 @@ public function matchesMethod(ReflectionMethod $method, array $arguments): bool
{
/** @var array<class-string> $arguments */
[$annotation] = $arguments;
$annotation = $this->reader->getMethodAnnotation($method, $annotation);
$annotation = ServiceLocator::getReader()->getMethodAnnotation($method, $annotation);

return (bool) $annotation;
}
Expand Down
19 changes: 19 additions & 0 deletions tests/AnnotatedMatcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Ray\Aop;

use PHPUnit\Framework\TestCase;

use function serialize;
use function unserialize;

class AnnotatedMatcherTest extends TestCase
{
public function testSerialize(): void
{
$object = new AnnotatedMatcher('startsWith', ['a']);
$this->assertInstanceOf(AnnotatedMatcher::class, unserialize(serialize($object)));
}
}
7 changes: 5 additions & 2 deletions tests/Fake/Annotation/FakeMarker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@
namespace Ray\Aop\Annotation;

use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;


/**
* @Annotation
* @Target("METHOD")
* @NamedArgumentConstructor
*/
#[Attribute(Attribute::TARGET_METHOD)]
final class FakeMarker
{
/** @var int */
public $value;

public function __construct($value)
public function __construct(int $value)
{
$this->value['value'] ?? $value;
$this->value = $value;
}
}
2 changes: 1 addition & 1 deletion tests/Fake/FakeAnnotateClassNoName.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class FakeAnnotateClassNoName
/**
* @FakeMarker3
* @FakeMarker2
* @FakeMarker
* @FakeMarker(1)
*/
public function getDouble($a)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Fake/FakeClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __toString()
}

/**
* @FakeMarker
* @FakeMarker(1)
*/
public function add($n)
{
Expand Down
3 changes: 0 additions & 3 deletions tests/ReflectiveMethodInvocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ public function testGetNamedArguments(): void
$this->assertSame((array) $args, ['n' => 1]);
}

/**
* @covers ReflectiveMethodInvocation::getNamedArguments
*/
public function testGetNamedArgumentsWithDefaultValue(): void
{
$invocation = new ReflectiveMethodInvocation(new FakeWeavedClass(), 'defaultValue', [1, null], [new FakeInterceptor(), new FakeInterceptor()]);
Expand Down
4 changes: 2 additions & 2 deletions vendor-bin/tools/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"require": {
"doctrine/coding-standard": "^8.2",
"require-dev": {
"doctrine/coding-standard": "^9.0",
"phpmd/phpmd": "^2.9",
"phpmetrics/phpmetrics": "^2.7",
"phpstan/phpstan": "^0.12",
Expand Down
Loading

0 comments on commit 1e92a1f

Please sign in to comment.