Skip to content

Commit cdad41e

Browse files
authored
Merge pull request #207 from koriym/phpstan12
Refactor with phpstan 0.12
2 parents cb1c24a + 1e7dd05 commit cdad41e

36 files changed

+237
-148
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ composer.lock
66
tmp/
77
.php_cs.cache
88
.phpunit.result.cache
9+
tests/script/grapher.php.txt

.php_cs.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ return \PhpCsFixer\Config::create()
134134
'phpdoc_inline_tag' => true, // @Symfony]
135135
'phpdoc_no_access' => true, // @Symfony]
136136
'phpdoc_no_alias_tag' => true, // @Symfony
137-
'phpdoc_no_empty_return' => true, // @Symfony
137+
// 'phpdoc_no_empty_return' => true, // @Symfony
138138
'phpdoc_no_package' => true, // @Symfony
139139
// 'phpdoc_no_useless_inheritdoc' => true, // @Symfony
140140
'phpdoc_order' => true,
@@ -143,7 +143,7 @@ return \PhpCsFixer\Config::create()
143143
'phpdoc_separation' => true, // @Symfony
144144
'phpdoc_single_line_var_spacing' => true, // @Symfony
145145
// 'phpdoc_summary' => true, // @Symfony
146-
'phpdoc_to_comment' => true, // @Symfony
146+
// 'phpdoc_to_comment' => true, // @Symfony
147147
'phpdoc_trim' => true, // @Symfony
148148
'phpdoc_trim_consecutive_blank_line_separation' => true,
149149
'phpdoc_types' => true, // @Symfony

.travis.yml

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,37 @@ script:
2121
- ./vendor/bin/phpunit;
2222

2323
jobs:
24+
fast_finish: true
2425
include:
25-
- stage: Test
26-
name: Lowest dependencies
27-
php: 7.2
28-
install: composer update --prefer-dist --prefer-lowest
29-
30-
- stage: Test
31-
name: Code coverage
32-
php: 7.2
33-
before_script:
34-
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{.disabled,}
35-
- if [[ ! $(php -m | grep -si xdebug) ]]; then echo "xdebug required for coverage"; exit 1; fi
36-
script:
37-
- ./vendor/bin/phpunit -v --coverage-clover ./build/logs/clover.xml
38-
after_script:
39-
- wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover coverage.clover;
40-
41-
- stage: Code Quality
42-
name: Static analysis
43-
php: 7.4
44-
install: composer require --dev phpstan/phpstan ^0.11 phpmetrics/phpmetrics vimeo/psalm
45-
script:
46-
- ./vendor/bin/phpstan analyse -l max -c phpstan.neon src tests --no-progress --no-interaction;
47-
- ./vendor/bin/psalm
48-
- ./vendor/bin/phpmetrics --excluded-dirs=src/Exception src
49-
50-
- stage: Code Quality
51-
name: Coding standards
52-
php: 7.4
53-
install: composer require --dev friendsofphp/php-cs-fixer ^2.0;
54-
script:
55-
- ./vendor/bin/php-cs-fixer --dry-run -v fix;
26+
- stage: Test
27+
name: Lowest dependencies
28+
php: 7.2
29+
install: composer update --prefer-dist --prefer-lowest
30+
31+
- stage: Code Quality
32+
name: Code coverage
33+
php: 7.2
34+
before_script:
35+
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{.disabled,}
36+
- if [[ ! $(php -m | grep -si xdebug) ]]; then echo "xdebug required for coverage"; exit 1; fi
37+
script:
38+
- ./vendor/bin/phpunit -v --coverage-clover ./build/logs/clover.xml
39+
after_script:
40+
- wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover coverage.clover;
41+
42+
- stage: Code Quality
43+
name: Static analysis
44+
php: 7.2
45+
install:
46+
- composer global require --dev phpstan/phpstan ^0.12 vimeo/psalm ^3.11 phpmetrics/phpmetrics ^2.6;
47+
script:
48+
- ~/.composer/vendor/bin/phpstan analyse -c phpstan.neon --no-progress --no-interaction;
49+
- ~/.composer/vendor/bin/psalm --show-info=false
50+
- ~/.composer/vendor/bin/phpmetrics --exclude=Exception src
51+
52+
- stage: Code Quality
53+
name: Coding standards
54+
php: 7.2
55+
install: composer global require --dev friendsofphp/php-cs-fixer ^2.0;
56+
script:
57+
- ~/.composer/vendor/bin/php-cs-fixer --dry-run -v fix;

composer.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
],
1717
"require": {
1818
"php": ">=7.2.0",
19-
"ray/aop": "^2.8.5",
20-
"ray/compiler": "1.x-dev|^1.3.6",
21-
"doctrine/annotations": "^1.7"
19+
"ray/aop": "^2.9.3",
20+
"ray/compiler": "1.x-dev|^1.3.6"
2221
},
2322
"require-dev": {
2423
"phpunit/phpunit": "^8.5"
@@ -35,10 +34,10 @@
3534
},
3635
"scripts" :{
3736
"test": ["phpunit"],
38-
"tests": ["@cs", "phpstan analyse -l max src tests -c phpstan.neon --no-progress", "psalm", "@test"],
37+
"tests": ["@cs", "phpstan analyse -c phpstan.neon --no-progress", "psalm", "@test"],
3938
"coverage": ["php -dzend_extension=xdebug.so ./vendor/bin/phpunit --coverage-text --coverage-html=build/coverage"],
4039
"cs": ["php-cs-fixer fix -v --dry-run", "phpcs --standard=./phpcs.xml src"],
4140
"cs-fix": ["php-cs-fixer fix -v", "phpcbf src"],
42-
"metrics": ["phpmetrics --report-html=build/metrics --excluded-dirs=src/Exception src"]
41+
"metrics": ["phpmetrics --report-html=build/metrics --exclude=Exception src"]
4342
}
4443
}

phpstan.neon

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
parameters:
2-
ignoreErrors:
3-
- '#does not call parent constructor from#'
4-
excludes_analyse:
5-
- %currentWorkingDirectory%/tests/tmp/*
2+
level: max
3+
paths:
4+
- src
5+
- tests
6+
excludes_analyse:
7+
- %currentWorkingDirectory%/tests/tmp/*
8+
- %currentWorkingDirectory%/tests/Fake/*
9+
checkMissingIterableValueType: false
10+
ignoreErrors:
11+
-
12+
message: "#Method Ray\\Di\\AbstractModule::getContainer() should return Ray\\Di\\Container but returns Ray\\Di\\Container|null\\.$#"
13+
path: src/AbstractModule.php
14+
-
15+
message: "#has no return typehint specified\\.$#"
16+
path: tests/*
17+
-
18+
message: "#expects class-string, string given\\.$#"
19+
path: tests/BindTest.php

src/AbstractModule.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Ray\Aop\AbstractMatcher;
88
use Ray\Aop\Matcher;
9+
use Ray\Aop\Pointcut;
910
use Ray\Aop\PriorityPointcut;
1011

1112
abstract class AbstractModule
@@ -21,7 +22,7 @@ abstract class AbstractModule
2122
protected $lastModule;
2223

2324
/**
24-
* @var Container
25+
* @var ?Container
2526
*/
2627
private $container;
2728

@@ -30,6 +31,7 @@ public function __construct(
3031
) {
3132
$this->lastModule = $module;
3233
$this->activate();
34+
assert($this->container instanceof Container);
3335
if ($module instanceof self) {
3436
$this->container->merge($module->getContainer());
3537
}
@@ -53,14 +55,12 @@ public function install(self $module) : void
5355
*/
5456
public function override(self $module) : void
5557
{
56-
$module->getContainer()->merge($this->container);
58+
$module->getContainer()->merge($this->getContainer());
5759
$this->container = $module->getContainer();
5860
}
5961

6062
/**
61-
* Return container
62-
*
63-
* @psalm-suppress DocblockTypeContradiction
63+
* Return activated container
6464
*/
6565
public function getContainer() : Container
6666
{
@@ -74,26 +74,28 @@ public function getContainer() : Container
7474
/**
7575
* Bind interceptor
7676
*
77-
* @param string[] $interceptors
77+
* @param array<class-string<\Ray\Aop\MethodInterceptor>> $interceptors
7878
*/
7979
public function bindInterceptor(AbstractMatcher $classMatcher, AbstractMatcher $methodMatcher, array $interceptors) : void
8080
{
8181
$pointcut = new Pointcut($classMatcher, $methodMatcher, $interceptors);
82-
$this->container->addPointcut($pointcut);
82+
$this->getContainer()->addPointcut($pointcut);
8383
foreach ($interceptors as $interceptor) {
84-
(new Bind($this->container, $interceptor))->to($interceptor)->in(Scope::SINGLETON);
84+
(new Bind($this->getContainer(), $interceptor))->to($interceptor)->in(Scope::SINGLETON);
8585
}
8686
}
8787

8888
/**
8989
* Bind interceptor early
90+
*
91+
* @param array<class-string<\Ray\Aop\MethodInterceptor>> $interceptors
9092
*/
9193
public function bindPriorityInterceptor(AbstractMatcher $classMatcher, AbstractMatcher $methodMatcher, array $interceptors) : void
9294
{
9395
$pointcut = new PriorityPointcut($classMatcher, $methodMatcher, $interceptors);
94-
$this->container->addPointcut($pointcut);
96+
$this->getContainer()->addPointcut($pointcut);
9597
foreach ($interceptors as $interceptor) {
96-
(new Bind($this->container, $interceptor))->to($interceptor)->in(Scope::SINGLETON);
98+
(new Bind($this->getContainer(), $interceptor))->to($interceptor)->in(Scope::SINGLETON);
9799
}
98100
}
99101

@@ -116,12 +118,14 @@ public function rename(string $interface, string $newName, string $sourceName =
116118
/**
117119
* Configure binding
118120
*
119-
* @psalm-suppress MissingReturnType
121+
* @return void
120122
*/
121123
abstract protected function configure();
122124

123125
/**
124126
* Bind interface
127+
*
128+
* @phpstan-param class-string|string $interface
125129
*/
126130
protected function bind(string $interface = '') : Bind
127131
{

src/AnnotatedClass.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(AnnotationReader $reader)
2828
/**
2929
* Return factory instance
3030
*
31-
* @param \ReflectionClass $class Target class reflection
31+
* @phpstan-param \ReflectionClass<object> $class Target class reflection
3232
*/
3333
public function getNewInstance(\ReflectionClass $class) : NewInstance
3434
{
@@ -47,6 +47,8 @@ public function getNewInstance(\ReflectionClass $class) : NewInstance
4747

4848
/**
4949
* Return @-PostConstruct method reflection
50+
*
51+
* @phpstan-param \ReflectionClass<object> $class
5052
*/
5153
public function getPostConstruct(\ReflectionClass $class) : ?\ReflectionMethod
5254
{

src/AnnotatedClassMethods.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public function __construct(Reader $reader)
2626
$this->nameKeyVarString = new NameKeyVarString($reader);
2727
}
2828

29+
/**
30+
* @phpstan-param \ReflectionClass<object> $class
31+
*/
2932
public function getConstructorName(\ReflectionClass $class) : Name
3033
{
3134
$constructor = $class->getConstructor();

src/Argument.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,19 @@ public function serialize() : string
102102
]);
103103
}
104104

105+
/**
106+
* @param string $serialized
107+
*
108+
* @throws \ReflectionException
109+
*/
105110
public function unserialize($serialized) : void
106111
{
107-
list($this->index,
112+
[$this->index,
108113
$this->isDefaultAvailable,
109114
$this->default,
110115
$this->meta,
111116
$ref
112-
) = unserialize($serialized);
117+
] = unserialize($serialized, ['allowed_classes' => false]);
113118
$this->reflection = new \ReflectionParameter([$ref[0], $ref[1]], $ref[2]);
114119
}
115120

@@ -129,7 +134,7 @@ private function setDefaultValue(\ReflectionParameter $parameter) : void
129134
private function getType(\ReflectionParameter $parameter) : string
130135
{
131136
$type = $parameter->getType();
132-
if (! $type instanceof \ReflectionType) {
137+
if (! $type instanceof \ReflectionNamedType) {
133138
return '';
134139
}
135140
if (\in_array($type->getName(), ['bool', 'int', 'string', 'array', 'resource', 'callable'], true)) {

src/AspectBind.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@ public function __construct(AopBind $bind)
2323
*/
2424
public function inject(Container $container) : array
2525
{
26+
/** @var array<array<class-string>> $bindings */
2627
$bindings = $this->bind->getBindings();
2728
foreach ($bindings as &$interceptors) {
28-
/* @var string[] $interceptors */
2929
foreach ($interceptors as &$interceptor) {
30-
if (\is_string($interceptor)) {
31-
$interceptor = $container->getInstance($interceptor, Name::ANY);
32-
}
30+
$interceptor = $container->getInstance($interceptor, Name::ANY);
3331
}
3432
}
3533

0 commit comments

Comments
 (0)