Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
242 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
tests/PhpStan/DynamicType/AbstractSprykerDynamicTypeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace PhpStan\DynamicType; | ||
|
||
use Exception; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
|
||
abstract class AbstractSprykerDynamicTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
protected $methodResolves = []; | ||
|
||
/** | ||
* @param MethodReflection $methodReflection | ||
* | ||
* @return bool | ||
*/ | ||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
if (isset($this->methodResolves[$methodReflection->getName()])) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @param MethodReflection $methodReflection | ||
* @param MethodCall $methodCall | ||
* @param Scope $scope | ||
* @return Type | ||
* | ||
* @throws Exception | ||
*/ | ||
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type | ||
{ | ||
$docComment = $scope->getClassReflection()->getNativeReflection()->getDocComment(); | ||
preg_match_all('#@method\s+(?:(?P<IsStatic>static)\s+)?(?:(?P<Type>[^\(\*]+?)(?<!\|)\s+)?(?P<MethodName>[a-zA-Z0-9_]+)(?P<Parameters>(?:\([^\)]*\))?)#', $docComment, $matches, PREG_SET_ORDER); | ||
|
||
foreach ($matches as $match) { | ||
if ($match['MethodName'] === $methodCall->name) { | ||
return new ObjectType($match['Type']); | ||
} | ||
} | ||
|
||
throw new Exception(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace PhpStan\DynamicType; | ||
|
||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use Spryker\Zed\Kernel\Communication\Console\Console; | ||
|
||
class ConsoleDynamicTypeExtension extends AbstractSprykerDynamicTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
protected $methodResolves = [ | ||
'getFacade' => true, | ||
'getQueryContainer' => true, | ||
'getFactory' => true, | ||
]; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public static function getClass(): string | ||
{ | ||
return Console::class; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
tests/PhpStan/DynamicType/ControllerDynamicTypeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace PhpStan\DynamicType; | ||
|
||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use Spryker\Zed\Kernel\Communication\Controller\AbstractController; | ||
|
||
class ControllerDynamicTypeExtension extends AbstractSprykerDynamicTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $methodResolves = [ | ||
'getFacade' => true, | ||
'getQueryContainer' => true, | ||
'getFactory' => true, | ||
]; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public static function getClass(): string | ||
{ | ||
return AbstractController::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace PhpStan\DynamicType; | ||
|
||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use Spryker\Zed\Kernel\Business\AbstractFacade; | ||
|
||
class FacadeDynamicTypeExtension extends AbstractSprykerDynamicTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
protected $methodResolves = [ | ||
'getFactory' => true, | ||
]; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public static function getClass(): string | ||
{ | ||
return AbstractFacade::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace PhpStan\DynamicType; | ||
|
||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use Spryker\Zed\Kernel\Communication\AbstractPlugin; | ||
|
||
class PluginDynamicTypeExtension extends AbstractSprykerDynamicTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $methodResolves = [ | ||
'getFacade' => true, | ||
'getFactory' => true, | ||
'getQueryContainer' => true, | ||
'getConfig' => true, | ||
]; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public static function getClass(): string | ||
{ | ||
return AbstractPlugin::class; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
tests/PhpStan/DynamicType/QueryContainerDynamicTypeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace PhpStan\DynamicType; | ||
|
||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use Spryker\Zed\Kernel\Persistence\AbstractQueryContainer; | ||
|
||
class QueryContainerDynamicTypeExtension extends AbstractSprykerDynamicTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $methodResolves = [ | ||
'getFactory' => true, | ||
]; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public static function getClass(): string | ||
{ | ||
return AbstractQueryContainer::class; | ||
} | ||
} |