Skip to content

Commit

Permalink
[DX] Add strict PHPStan rules - step #4 (#1333)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Nov 28, 2021
1 parent 6340d26 commit 185ee1b
Show file tree
Hide file tree
Showing 35 changed files with 109 additions and 85 deletions.
9 changes: 7 additions & 2 deletions packages/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private function printPhpDocNode(PhpDocNode $phpDocNode): string
if (StringUtils::isMatch(
$output,
self::OPENING_DOCBLOCK_REGEX
) && $output && ! StringUtils::isMatch($output, self::CLOSING_DOCBLOCK_REGEX)) {
) && ! StringUtils::isMatch($output, self::CLOSING_DOCBLOCK_REGEX)) {
$output .= ' */';
}

Expand Down Expand Up @@ -247,7 +247,12 @@ private function printEnd(string $output): string
{
$lastTokenPosition = $this->getCurrentPhpDocInfo()
->getPhpDocNode()
->getAttribute(PhpDocAttributeKey::LAST_PHP_DOC_TOKEN_POSITION) ?: $this->currentTokenPosition;
->getAttribute(PhpDocAttributeKey::LAST_PHP_DOC_TOKEN_POSITION);

if ($lastTokenPosition === null) {
$lastTokenPosition = $this->currentTokenPosition;
}

if ($lastTokenPosition === 0) {
$lastTokenPosition = 1;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/Caching/Config/FileHashComputer.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private function createFileLoader(string $filePath, ContainerBuilder $containerB

$loaderResolver = new LoaderResolver($fileLoaders);
$loader = $loaderResolver->resolve($filePath);
if (! $loader) {
if ($loader === false) {
throw new ShouldNotHappenException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private function reportFileDiffs(array $fileDiffs): void
$message = sprintf('<options=bold>%d) %s</>', ++$i, $relativeFilePath);

$this->outputStyle->writeln($message);
$this->outputStyle->newLine();
$this->outputStyle->newline();
$this->outputStyle->writeln($fileDiff->getDiffConsoleFormatted());

$rectorsChangelogsLines = $this->createRectorChangelogLines($fileDiff);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\NodeTypeResolver\NodeTypeResolver;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use PHPStan\Analyser\Scope;
Expand All @@ -17,6 +18,8 @@

/**
* @see \Rector\Tests\NodeTypeResolver\PerNodeTypeResolver\VariableTypeResolver\VariableTypeResolverTest
*
* @implements NodeTypeResolverInterface<Variable>
*/
final class VariableTypeResolver implements NodeTypeResolverInterface
{
Expand All @@ -27,7 +30,7 @@ public function __construct(
}

/**
* @return array<class-string<Node>>
* @return array<class-string<Expr>>
*/
public function getNodeClasses(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ private function isAlwaysTruableUnionType(Type $type): bool
private function isAlwaysTruableArrayType(ArrayType $arrayType): bool
{
$itemType = $arrayType->getItemType();
return $itemType instanceof ConstantScalarType && $itemType->getValue();
if (! $itemType instanceof ConstantScalarType) {
return false;
}

return (bool) $itemType->getValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function provide(): SourceLocator
// do not cache for PHPUnit, as in test every fixture is different
$isPHPUnitRun = StaticPHPUnitEnvironment::isPHPUnitRun();

if ($this->aggregateSourceLocator && ! $isPHPUnitRun) {
if ($this->aggregateSourceLocator instanceof AggregateSourceLocator && ! $isPHPUnitRun) {
return $this->aggregateSourceLocator;
}

Expand Down
5 changes: 1 addition & 4 deletions packages/Parallel/Application/ParallelFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,7 @@ function (array $json) use (
$fileDiffs[] = FileDiff::decode($jsonError);
}

// @todo why there is a null check?
if ($postFileCallback !== null) {
$postFileCallback($json[Bridge::FILES_COUNT]);
}
$postFileCallback($json[Bridge::FILES_COUNT]);

$systemErrorsCount += $json[Bridge::SYSTEM_ERRORS_COUNT];
if ($systemErrorsCount >= self::SYSTEM_ERROR_COUNT_LIMIT) {
Expand Down
12 changes: 9 additions & 3 deletions packages/ReadWrite/Contract/ReadNodeAnalyzerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

namespace Rector\ReadWrite\Contract;

use PhpParser\Node;
use PhpParser\Node\Expr;

/**
* @template TExpr as Expr
*/
interface ReadNodeAnalyzerInterface
{
public function supports(Node $node): bool;
public function supports(Expr $expr): bool;

public function isRead(Node $node): bool;
/**
* @param TExpr $expr
*/
public function isRead(Expr $expr): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\ReadWrite\ReadNodeAnalyzer;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Stmt\Class_;
Expand All @@ -13,6 +14,9 @@
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\ReadWrite\Contract\ReadNodeAnalyzerInterface;

/**
* @implements ReadNodeAnalyzerInterface<PropertyFetch|StaticPropertyFetch>
*/
final class LocalPropertyFetchReadNodeAnalyzer implements ReadNodeAnalyzerInterface
{
public function __construct(
Expand All @@ -23,23 +27,20 @@ public function __construct(
) {
}

public function supports(Node $node): bool
public function supports(Expr $expr): bool
{
return $node instanceof PropertyFetch || $node instanceof StaticPropertyFetch;
return $expr instanceof PropertyFetch || $expr instanceof StaticPropertyFetch;
}

/**
* @param PropertyFetch $node
*/
public function isRead(Node $node): bool
public function isRead(Expr $expr): bool
{
$class = $this->betterNodeFinder->findParentType($node, Class_::class);
$class = $this->betterNodeFinder->findParentType($expr, Class_::class);
if (! $class instanceof Class_) {
// assume worse to keep node protected
return true;
}

$propertyName = $this->nodeNameResolver->getName($node->name);
$propertyName = $this->nodeNameResolver->getName($expr->name);
if ($propertyName === null) {
// assume worse to keep node protected
return true;
Expand Down
17 changes: 10 additions & 7 deletions packages/ReadWrite/ReadNodeAnalyzer/VariableReadNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

namespace Rector\ReadWrite\ReadNodeAnalyzer;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Variable;
use Rector\NodeNestingScope\ParentScopeFinder;
use Rector\ReadWrite\Contract\ReadNodeAnalyzerInterface;
use Rector\ReadWrite\NodeFinder\NodeUsageFinder;

/**
* @implements ReadNodeAnalyzerInterface<Variable>
*/
final class VariableReadNodeAnalyzer implements ReadNodeAnalyzerInterface
{
public function __construct(
Expand All @@ -19,22 +22,22 @@ public function __construct(
) {
}

public function supports(Node $node): bool
public function supports(Expr $expr): bool
{
return $node instanceof Variable;
return $expr instanceof Variable;
}

/**
* @param Variable $node
* @param Variable $expr
*/
public function isRead(Node $node): bool
public function isRead(Expr $expr): bool
{
$parentScope = $this->parentScopeFinder->find($node);
$parentScope = $this->parentScopeFinder->find($expr);
if ($parentScope === null) {
return false;
}

$variableUsages = $this->nodeUsageFinder->findVariableUsages((array) $parentScope->stmts, $node);
$variableUsages = $this->nodeUsageFinder->findVariableUsages((array) $parentScope->stmts, $expr);
foreach ($variableUsages as $variableUsage) {
if ($this->justReadExprAnalyzer->isReadContext($variableUsage)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Type;

/**
* @template TTypeNode as TypeNode
*/
interface PhpDocTypeMapperInterface
{
/**
* @return class-string<TypeNode>
* @return class-string<TTypeNode>
*/
public function getNodeType(): string;

/**
* @param TTypeNode $typeNode
*/
public function mapToPHPStanType(TypeNode $typeNode, Node $node, NameScope $nameScope): Type;
}
8 changes: 1 addition & 7 deletions packages/StaticTypeMapper/Naming/NameScopeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\StaticTypeMapper\StaticTypeMapper;
Expand Down Expand Up @@ -96,15 +95,10 @@ private function resolveUseNamesByAlias(array $useNodes): array
$aliasName = $useUse->getAlias()
->name;

$useName = $useUse->name->toString();
if (! is_string($useName)) {
throw new ShouldNotHappenException();
}

// uses must be lowercase, as PHPStan lowercases it
$lowercasedAliasName = strtolower($aliasName);

$useNamesByAlias[$lowercasedAliasName] = $useName;
$useNamesByAlias[$lowercasedAliasName] = $useUse->name->toString();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
use Rector\StaticTypeMapper\ValueObject\Type\SelfObjectType;
use Rector\TypeDeclaration\PHPStan\Type\ObjectTypeSpecifier;

/**
* @implements PhpDocTypeMapperInterface<IdentifierTypeNode>
*/
final class IdentifierTypeMapper implements PhpDocTypeMapperInterface
{
public function __construct(
Expand All @@ -38,9 +41,6 @@ public function __construct(
) {
}

/**
* @return class-string<TypeNode>
*/
public function getNodeType(): string
{
return IdentifierTypeNode::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
use Rector\StaticTypeMapper\PhpDoc\PhpDocTypeMapper;
use Symfony\Contracts\Service\Attribute\Required;

/**
* @implements PhpDocTypeMapperInterface<IntersectionTypeNode>
*/
final class IntersectionTypeMapper implements PhpDocTypeMapperInterface
{
private PhpDocTypeMapper $phpDocTypeMapper;

/**
* @return class-string<TypeNode>
*/
public function getNodeType(): string
{
return IntersectionTypeNode::class;
Expand Down
9 changes: 3 additions & 6 deletions packages/StaticTypeMapper/PhpDocParser/NullableTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use PHPStan\Type\UnionType;
use Rector\StaticTypeMapper\Contract\PhpDocParser\PhpDocTypeMapperInterface;

/**
* @implements PhpDocTypeMapperInterface<NullableTypeNode>
*/
final class NullableTypeMapper implements PhpDocTypeMapperInterface
{
public function __construct(
Expand All @@ -23,17 +26,11 @@ public function __construct(
) {
}

/**
* @return class-string<TypeNode>
*/
public function getNodeType(): string
{
return NullableTypeNode::class;
}

/**
* @param NullableTypeNode $typeNode
*/
public function mapToPHPStanType(TypeNode $typeNode, Node $node, NameScope $nameScope): Type
{
$type = $typeNode->type;
Expand Down
6 changes: 3 additions & 3 deletions packages/StaticTypeMapper/PhpDocParser/UnionTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Rector\StaticTypeMapper\PhpDoc\PhpDocTypeMapper;
use Symfony\Contracts\Service\Attribute\Required;

/**
* @implements PhpDocTypeMapperInterface<UnionTypeNode>
*/
final class UnionTypeMapper implements PhpDocTypeMapperInterface
{
private PhpDocTypeMapper $phpDocTypeMapper;
Expand All @@ -23,9 +26,6 @@ public function __construct(
) {
}

/**
* @return class-string<TypeNode>
*/
public function getNodeType(): string
{
return UnionTypeNode::class;
Expand Down
3 changes: 0 additions & 3 deletions packages/StaticTypeMapper/PhpParser/ExprNodeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
*/
final class ExprNodeMapper implements PhpParserNodeMapperInterface
{
/**
* @return class-string<Node>
*/
public function getNodeType(): string
{
return Expr::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ public function __construct(
) {
}

/**
* @return class-string<Node>
*/
public function getNodeType(): string
{
return FullyQualified::class;
Expand Down
3 changes: 0 additions & 3 deletions packages/StaticTypeMapper/PhpParser/IdentifierNodeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ public function __construct(
) {
}

/**
* @return class-string<Node>
*/
public function getNodeType(): string
{
return Identifier::class;
Expand Down
3 changes: 0 additions & 3 deletions packages/StaticTypeMapper/PhpParser/NameNodeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ public function __construct(
) {
}

/**
* @return class-string<Node>
*/
public function getNodeType(): string
{
return Name::class;
Expand Down
3 changes: 0 additions & 3 deletions packages/StaticTypeMapper/PhpParser/StringNodeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
*/
final class StringNodeMapper implements PhpParserNodeMapperInterface
{
/**
* @return class-string<Node>
*/
public function getNodeType(): string
{
return String_::class;
Expand Down

0 comments on commit 185ee1b

Please sign in to comment.