Skip to content

Commit

Permalink
Add phpStan extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilinTv committed Oct 24, 2017
1 parent 42755cb commit 15d79da
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -94,7 +94,8 @@
},
"autoload-dev": {
"psr-0": {
"PyzTest": "tests"
"PyzTest": "tests",
"PhpStan": "tests"
},
"files": [
"test-autoload.php"
Expand Down
26 changes: 26 additions & 0 deletions phpstan.neon
Expand Up @@ -4,3 +4,29 @@ parameters:
- %rootDir%/../../../src/Orm/*

bootstrap: %rootDir%/../../../phpstan-bootstrap.php

services:
-
class: PhpStan\DynamicType\FacadeDynamicTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

-
class: PhpStan\DynamicType\QueryContainerDynamicTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

-
class: PhpStan\DynamicType\ConsoleDynamicTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

-
class: PhpStan\DynamicType\ControllerDynamicTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

-
class: PhpStan\DynamicType\PluginDynamicTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
11 changes: 11 additions & 0 deletions test-autoload.php
Expand Up @@ -21,6 +21,17 @@
];
}

if ($classNameParts[0] === 'PhpStan') {
array_shift($classNameParts);
$className = implode(DIRECTORY_SEPARATOR, $classNameParts);
$filePathPartsSupport = [
__DIR__,
'tests',
'PhpStan',
$className . '.php',
];
}

if (isset($filePathPartsSupport)) {
$filePath = implode(DIRECTORY_SEPARATOR, $filePathPartsSupport);
if (file_exists($filePath)) {
Expand Down
57 changes: 57 additions & 0 deletions tests/PhpStan/DynamicType/AbstractSprykerDynamicTypeExtension.php
@@ -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();
}
}
28 changes: 28 additions & 0 deletions tests/PhpStan/DynamicType/ConsoleDynamicTypeExtension.php
@@ -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 tests/PhpStan/DynamicType/ControllerDynamicTypeExtension.php
@@ -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;
}
}
26 changes: 26 additions & 0 deletions tests/PhpStan/DynamicType/FacadeDynamicTypeExtension.php
@@ -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;
}
}
32 changes: 32 additions & 0 deletions tests/PhpStan/DynamicType/PluginDynamicTypeExtension.php
@@ -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 tests/PhpStan/DynamicType/QueryContainerDynamicTypeExtension.php
@@ -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;
}
}

0 comments on commit 15d79da

Please sign in to comment.