Skip to content

Commit

Permalink
bump to PHPStan 0.12
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Dec 8, 2019
1 parent bace233 commit 9075363
Show file tree
Hide file tree
Showing 37 changed files with 325 additions and 113 deletions.
5 changes: 0 additions & 5 deletions compiler/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,5 @@
},
"require-dev": {
"phpunit/phpunit": "^8.4"
},
"extra": {
"cleaner-ignore": {
"phpstan/phpstan": ["conf"]
}
}
}
3 changes: 0 additions & 3 deletions compiler/src/Console/CompileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->processFactory->setOutput($output);

// this breaks phpstan dependency by removing whole "/conf" directory - https://github.com/dg/composer-cleaner#configuration
$this->processFactory->create(['composer', 'require', '--no-update', 'dg/composer-cleaner:^2.0'], $this->buildDir);

$composerJsonFile = $this->buildDir . '/composer.json';

$this->fixComposerJson($composerJsonFile);
Expand Down
14 changes: 8 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"nette/utils": "^3.0",
"nikic/php-parser": "^4.3",
"ondram/ci-detector": "^3.1",
"phpstan/phpdoc-parser": "^0.3.5",
"phpstan/phpstan": "0.11.19",
"phpstan/phpstan-phpunit": "^0.11.2",
"phpstan/phpdoc-parser": "0.4 as 0.3.5",
"phpstan/phpstan": "0.12 as 0.11.19",
"phpstan/phpstan-phpunit": "0.12 as 0.11.2",
"sebastian/diff": "^3.0",
"symfony/console": "^4.4|^5.0",
"symfony/dependency-injection": "^4.4|^5.0",
Expand All @@ -40,7 +40,7 @@
"symplify/easy-coding-standard": "^7.0",
"symplify/monorepo-builder": "^7.0",
"symplify/phpstan-extensions": "^7.0",
"thecodingmachine/phpstan-strict-rules": "^0.11.2",
"thecodingmachine/phpstan-strict-rules": "^0.12",
"tracy/tracy": "^2.7"
},
"autoload": {
Expand Down Expand Up @@ -199,7 +199,7 @@
"ecs check bin packages src tests utils compiler --fix --ansi",
"ci/clean_trailing_spaces.sh"
],
"phpstan": "phpstan analyse packages src tests compiler/src --error-format symplify --ansi",
"phpstan": "phpstan analyse packages src tests compiler/src --ansi",
"changelog": [
"changelog-linker dump-merges --in-categories",
"changelog-linker link",
Expand All @@ -225,5 +225,7 @@
"branch-alias": {
"dev-master": "0.6-dev"
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
2 changes: 1 addition & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ services:
Symplify\PackageBuilder\Strings\StringFormatConverter: ~

# alises
Symfony\Component\EventDispatcher\EventDispatcherInterface: '@Rector\EventDispatcher\AutowiredEventDispatcher'
Symfony\Contracts\EventDispatcher\EventDispatcherInterface: '@Rector\EventDispatcher\AutowiredEventDispatcher'
Rector\Bridge\Contract\AnalyzedApplicationContainerInterface: '@Rector\Symfony\Bridge\DefaultAnalyzedSymfonyApplicationContainer'

OndraM\CiDetector\CiDetector: ~
Expand Down
2 changes: 1 addition & 1 deletion docs/AllRectorsOverview.md
Original file line number Diff line number Diff line change
Expand Up @@ -6768,7 +6768,7 @@ Make Symfony commands lazy
Make event object a first argument of dispatch() method, event name as second

```diff
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class SomeClass
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,53 @@
<?php

namespace Rector\CodeQuality\Tests\Rector\If_\SimplifyIfReturnBoolRector\Fixture;

use PhpCsFixer\Tokenizer\Token;

function simplifyIfReturnBool()
class SomeClass
{
$docToken = new Token([]);
if (strpos($docToken->getContent(), "\n") === false) {
return true;
public function first()
{
$docToken = new Token([]);
if (strpos($docToken->getContent(), "\n") === false) {
return true;
}

return false;
}

return false;

public function second()
{
$docToken = new Token([]);
if (strpos($docToken->getContent(), "\n") !== false) {
return true;
}

if (strpos($docToken->getContent(), "\n") !== false) {
return true;
return false;
}

return false;
}

?>
-----
<?php

namespace Rector\CodeQuality\Tests\Rector\If_\SimplifyIfReturnBoolRector\Fixture;

use PhpCsFixer\Tokenizer\Token;

function simplifyIfReturnBool()
class SomeClass
{
$docToken = new Token([]);
return strpos($docToken->getContent(), "\n") === false;
return strpos($docToken->getContent(), "\n") !== false;
public function first()
{
$docToken = new Token([]);
return strpos($docToken->getContent(), "\n") === false;
}

public function second()
{
$docToken = new Token([]);
return strpos($docToken->getContent(), "\n") !== false;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@ class EdgeCases
{
$order = $someQueryBuilder->getQuery()->getOneOrNullResult();
return $order ? true : false;
}

public function go(): bool
{
$order = $someQueryBuilder->getQuery()->getOneOrNullResult();
return $order ? false : true;
}

public function boolAlready(): bool
{
$order = false;
return $order ? true : false;
}

public function switchBack(): bool
{
$order = false;
return $order ? false : true;
}
}
Expand All @@ -33,15 +41,23 @@ class EdgeCases
{
$order = $someQueryBuilder->getQuery()->getOneOrNullResult();
return (bool) $order;
}

public function go(): bool
{
$order = $someQueryBuilder->getQuery()->getOneOrNullResult();
return !(bool) $order;
}

public function boolAlready(): bool
{
$order = false;
return $order;
}

public function switchBack(): bool
{
$order = false;
return !$order;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Rector\DeadCode\Tests\Rector\ClassMethod\RemoveDelegatingParentCallRector\Fixture;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class SkipRequired extends ParentClassWithoutRequired
{
Expand Down
4 changes: 2 additions & 2 deletions packages/NetteTesterToPHPUnit/src/AssertManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,11 @@ private function processTypeCall(StaticCall $staticCall): void
if (isset($typeToMethod[$value])) {
$staticCall->name = new Identifier($typeToMethod[$value]);
unset($staticCall->args[0]);
array_values($staticCall->args);
$staticCall->args = array_values($staticCall->args);
} elseif ($value === 'null') {
$staticCall->name = new Identifier('assertNull');
unset($staticCall->args[0]);
array_values($staticCall->args);
$staticCall->args = array_values($staticCall->args);
} else {
$staticCall->name = new Identifier('assertInstanceOf');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ private function resolvePathFromClassAndMethodNodes(Class_ $classNode, ClassMeth
$presenterPart = Strings::substring($presenterPart, 0, -Strings::length('Presenter'));
$presenterPart = RectorStrings::camelCaseToDashes($presenterPart);

$match = Strings::match($this->getName($classMethod), '#^(action|render)(?<short_action_name>.*?$)#sm');
$match = (array) Strings::match($this->getName($classMethod), '#^(action|render)(?<short_action_name>.*?$)#sm');
$actionPart = lcfirst($match['short_action_name']);

return $presenterPart . '/' . $actionPart;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Rector\NodeTypeResolver\DependencyInjection;

use Nette\DI\Container;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Analyser\ScopeFactory;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Broker\Broker;
use PHPStan\DependencyInjection\Container;
use PHPStan\DependencyInjection\ContainerFactory;

final class PHPStanServicesFactory
Expand Down
24 changes: 21 additions & 3 deletions packages/NodeTypeResolver/src/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
Expand All @@ -36,6 +37,8 @@
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
Expand Down Expand Up @@ -326,6 +329,23 @@ public function getStaticType(Node $node): Type

/** @var Scope|null $nodeScope */
$nodeScope = $node->getAttribute(AttributeKey::SCOPE);

if ($node instanceof Scalar) {
if ($nodeScope === null) {
if ($node instanceof Node\Scalar\DNumber) {
return new ConstantFloatType($node->value);
}

if ($node instanceof Node\Scalar\String_) {
return new ConstantStringType($node->value);
}

if ($node instanceof Node\Scalar\LNumber) {
return new ConstantIntegerType($node->value);
}
}
}

if (! $node instanceof Expr || $nodeScope === null) {
return new MixedType();
}
Expand All @@ -336,9 +356,6 @@ public function getStaticType(Node $node): Type
}
}

// make object type specific to alias or FQN
$staticType = $nodeScope->getType($node);

// false type correction of inherited method
if ($node instanceof MethodCall) {
if ($this->isObjectType($node->var, SplFileInfo::class)) {
Expand All @@ -349,6 +366,7 @@ public function getStaticType(Node $node): Type
}
}

$staticType = $nodeScope->getType($node);
if (! $staticType instanceof ObjectType) {
return $staticType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Trait_;
use PhpParser\NodeTraverser;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver as PHPStanNodeScopeResolver;
use PHPStan\Analyser\Scope;
use PHPStan\Broker\Broker;
Expand Down Expand Up @@ -75,7 +76,7 @@ public function processNodes(array $nodes, string $filePath): array
$scope = $this->scopeFactory->createFromFile($filePath);

// skip chain method calls, performance issue: https://github.com/phpstan/phpstan/issues/254
$nodeCallback = function (Node $node, Scope $scope): void {
$nodeCallback = function (Node $node, MutatingScope $scope): void {
// the class reflection is resolved AFTER entering to class node
// so we need to get it from the first after this one
if ($node instanceof Class_ || $node instanceof Interface_) {
Expand All @@ -100,6 +101,7 @@ public function processNodes(array $nodes, string $filePath): array
}
};

/** @var MutatingScope $scope */
$this->phpStanNodeScopeResolver->processNodes($nodes, $scope, $nodeCallback);

return $nodes;
Expand All @@ -118,7 +120,7 @@ private function removeDeepChainMethodCallNodes(array $nodes): void
/**
* @param Class_|Interface_ $classOrInterfaceNode
*/
private function resolveClassOrInterfaceScope(Node $classOrInterfaceNode, Scope $scope): Scope
private function resolveClassOrInterfaceScope(Node $classOrInterfaceNode, MutatingScope $scope): MutatingScope
{
$className = $this->resolveClassName($classOrInterfaceNode);
$classReflection = $this->broker->getClass($className);
Expand Down
3 changes: 2 additions & 1 deletion packages/NodeTypeResolver/src/PHPStan/Scope/ScopeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\NodeTypeResolver\PHPStan\Scope;

use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\ScopeContext;
use PHPStan\Analyser\ScopeFactory as PHPStanScopeFactory;
Expand Down Expand Up @@ -48,7 +49,7 @@ public function __construct(

public function createFromFile(string $filePath): Scope
{
return new Scope(
return new MutatingScope(
$this->phpStanScopeFactory,
$this->broker,
$this->betterStandardPrinter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ function runContinueZeroes($random)
{
continue 0;
break 0;
}

function breakIt()
{
$five = 5;
continue $five;

Expand All @@ -23,7 +26,10 @@ function runContinueZeroes($random)
{
continue;
break;
}

function breakIt()
{
$five = 5;
continue 5;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function refactor(Node $node): ?Node
$defaultCases[] = $case;
}

if ($defaultCases < 2) {
if (count($defaultCases) < 2) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Rector\Php71\Tests\Rector\FuncCall\RemoveExtraParametersRector\Source;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

final class MagicEventDispatcher
{
Expand Down
Loading

0 comments on commit 9075363

Please sign in to comment.