Implement Codeception extension for PHPStan
[MAILPOET-2429]
This commit is contained in:
committed by
Jack Kitterhing
parent
66c48f4690
commit
5c2cf6e3c7
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\PHPStan\Extensions\CodeceptionExtension\Type;
|
||||
|
||||
use Codeception\Stub;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Reflection\MethodReflection;
|
||||
use PHPStan\Type\Constant\ConstantStringType;
|
||||
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use PHPStan\Type\Type;
|
||||
|
||||
class StubDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension {
|
||||
|
||||
public function getClass(): string {
|
||||
return Stub::class;
|
||||
}
|
||||
|
||||
public function isStaticMethodSupported(MethodReflection $reflection): bool {
|
||||
return in_array($reflection->getName(), [
|
||||
'make',
|
||||
'makeEmpty',
|
||||
'makeEmptyExcept',
|
||||
'construct',
|
||||
'constructEmpty',
|
||||
'constructEmptyExcept',
|
||||
'copy',
|
||||
], true);
|
||||
}
|
||||
|
||||
public function getTypeFromStaticMethodCall(MethodReflection $reflection, StaticCall $call, Scope $scope): Type {
|
||||
$type = $scope->getType($call->args[0]->value);
|
||||
if ($type instanceof ConstantStringType) {
|
||||
return new ObjectType($type->getValue()); // $type is class name
|
||||
}
|
||||
return $type;
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\PHPStan\Extensions\CodeceptionExtension\Type;
|
||||
|
||||
use Codeception\Test\Unit;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Reflection\MethodReflection;
|
||||
use PHPStan\Type\Constant\ConstantStringType;
|
||||
use PHPStan\Type\DynamicMethodReturnTypeExtension;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use PHPStan\Type\Type;
|
||||
|
||||
class TestCaseDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension {
|
||||
public function getClass(): string {
|
||||
return Unit::class;
|
||||
}
|
||||
|
||||
public function isMethodSupported(MethodReflection $reflection): bool {
|
||||
return in_array($reflection->getName(), [
|
||||
'make',
|
||||
'makeEmpty',
|
||||
'makeEmptyExcept',
|
||||
'construct',
|
||||
'constructEmpty',
|
||||
'constructEmptyExcept',
|
||||
'copy',
|
||||
], true);
|
||||
}
|
||||
|
||||
public function getTypeFromMethodCall(MethodReflection $reflection, MethodCall $call, Scope $scope): Type {
|
||||
$type = $scope->getType($call->args[0]->value);
|
||||
if ($type instanceof ConstantStringType) {
|
||||
return new ObjectType($type->getValue()); // $type is class name
|
||||
}
|
||||
return $type;
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
services:
|
||||
-
|
||||
class: MailPoet\PHPStan\Extensions\CodeceptionExtension\Type\StubDynamicReturnTypeExtension
|
||||
tags:
|
||||
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
|
||||
-
|
||||
class: MailPoet\PHPStan\Extensions\CodeceptionExtension\Type\TestCaseDynamicReturnTypeExtension
|
||||
tags:
|
||||
- phpstan.broker.dynamicMethodReturnTypeExtension
|
@ -15,3 +15,4 @@ parameters:
|
||||
includes:
|
||||
- vendor/phpstan/phpstan-phpunit/extension.neon
|
||||
- extensions/PHPUnit5CompatExtension/extension.neon
|
||||
- extensions/CodeceptionExtension/extension.neon
|
||||
|
Reference in New Issue
Block a user