Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ matrix:
env: CODING_STANDARD=true
- php: '7.2'
env: STANDALONE=true
- php: '7.3'
env: DOG_FOOD=true

install:
- composer update $COMPOSER_FLAGS
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
"bin/rector dump-rectors -o markdown > docs/AllRectorsOverview.md",
"bin/rector dump-nodes -o markdown > docs/NodesOverview.md"
],
"rector": "bin/rector process packages src --config rector-ci.yaml --dry-run"
"rector": "bin/rector process packages src tests --config rector-ci.yaml --dry-run"
},
"scripts-descriptions": {
"docs": "Regenerate descriptions of all Rectors to docs/AllRectorsOverview.md file"
Expand Down
3 changes: 3 additions & 0 deletions packages/BetterPhpDocParser/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ services:

PHPStan\PhpDocParser\Parser\PhpDocParser:
alias: 'Rector\BetterPhpDocParser\PhpDocParser\BetterPhpDocParser'

Doctrine\Common\Annotations\Reader:
alias: 'Doctrine\Common\Annotations\AnnotationReader'
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\Reader;

final class AnnotationReaderFactory
{
public function create(): AnnotationReader
public function create(): Reader
{
AnnotationRegistry::registerLoader('class_exists');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
namespace Rector\BetterPhpDocParser\AnnotationReader;

use Doctrine\Common\Annotations\AnnotationException;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ORM\Mapping\Annotation;
use Doctrine\Common\Annotations\Reader;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
Expand All @@ -16,24 +15,23 @@
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use Symfony\Component\Validator\Constraint;
use Throwable;

final class NodeAnnotationReader
{
/**
* @var AnnotationReader
* @var Reader
*/
private $annotationReader;
private $reader;

/**
* @var NameResolver
*/
private $nameResolver;

public function __construct(AnnotationReader $annotationReader, NameResolver $nameResolver)
public function __construct(Reader $reader, NameResolver $nameResolver)
{
$this->annotationReader = $annotationReader;
$this->reader = $reader;
$this->nameResolver = $nameResolver;
}

Expand All @@ -51,7 +49,7 @@ public function readMethodAnnotation(ClassMethod $classMethod, string $annotatio
$reflectionMethod = new ReflectionMethod($className, $methodName);

try {
return $this->annotationReader->getMethodAnnotation($reflectionMethod, $annotationClassName);
return $this->reader->getMethodAnnotation($reflectionMethod, $annotationClassName);
} catch (AnnotationException $annotationException) {
// unable to laod
return null;
Expand All @@ -65,11 +63,11 @@ public function readClassAnnotation(Class_ $class, string $annotationClassName)
{
$classReflection = $this->createClassReflectionFromNode($class);

return $this->annotationReader->getClassAnnotation($classReflection, $annotationClassName);
return $this->reader->getClassAnnotation($classReflection, $annotationClassName);
}

/**
* @return Annotation|Constraint|null
* @return object|null
*/
public function readPropertyAnnotation(Property $property, string $annotationClassName)
{
Expand All @@ -78,13 +76,7 @@ public function readPropertyAnnotation(Property $property, string $annotationCla
return null;
}

/** @var Annotation|null $propertyAnnotation */
$propertyAnnotation = $this->annotationReader->getPropertyAnnotation($propertyReflection, $annotationClassName);
if ($propertyAnnotation === null) {
return null;
}

return $propertyAnnotation;
return $this->reader->getPropertyAnnotation($propertyReflection, $annotationClassName);
}

private function createClassReflectionFromNode(Class_ $class): ReflectionClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,19 @@ public function __toString(): string
$contentItems['name'] = sprintf('name="%s"', $this->name);
}

if ($this->methods) {
if ($this->methods !== []) {
$contentItems['methods'] = $this->printArrayItem($this->methods, 'methods');
}

if ($this->options) {
if ($this->options !== []) {
$contentItems['options'] = $this->printArrayItem($this->options, 'options');
}

if ($this->defaults) {
if ($this->defaults !== []) {
$contentItems['defaults'] = $this->printArrayItem($this->defaults, 'defaults');
}

if ($this->requirements) {
if ($this->requirements !== []) {
$contentItems['requirements'] = $this->printArrayItem($this->requirements, 'requirements');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ public function createFromNodeAndTokens(Node $node, TokenIterator $tokenIterator
return null;
}

if ($inject->value === null) {
$serviceName = $this->nameResolver->getName($node);
} else {
$serviceName = $inject->value;
}
$serviceName = $inject->value === null ? $this->nameResolver->getName($node) : $inject->value;

// needed for proper doc block formatting
$this->resolveContentFromTokenIterator($tokenIterator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function resolveNestedKey(string $annotationContent, string $name): strin
}

$start = $this->tryStartWithKey($name, $start, $tokenIterator);
if ($start === false) {
if (! $start) {
$tokenIterator->next();
continue;
}
Expand Down Expand Up @@ -112,7 +112,7 @@ private function cleanMultilineAnnotationContent(string $annotationContent): str

private function tryStartWithKey(string $name, bool $start, TokenIterator $localTokenIterator): bool
{
if ($start === true) {
if ($start) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,7 @@ private function isTagMatchingPhpDocNodeFactory(
$tag = ltrim($tag, '@');

if ($phpDocNodeFactory instanceof NameAwarePhpDocNodeFactoryInterface) {
if (Strings::lower($phpDocNodeFactory->getName()) === Strings::lower($tag)) {
return true;
}

return false;
return Strings::lower($phpDocNodeFactory->getName()) === Strings::lower($tag);
}

if ($phpDocNodeFactory instanceof ClassAwarePhpDocNodeFactoryInterface) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private function matchFullAnnotationClassWithUses(string $tag, array $uses): ?st
continue;
}

if ($useUse->alias) {
if ($useUse->alias !== null) {
$unaliasedShortClass = Strings::substring($tag, Strings::length($useUse->alias->toString()));
if (Strings::startsWith($unaliasedShortClass, '\\')) {
return $useUse->name . $unaliasedShortClass;
Expand All @@ -62,7 +62,7 @@ private function matchFullAnnotationClassWithUses(string $tag, array $uses): ?st

private function isUseMatchingName(string $tag, UseUse $useUse): bool
{
$shortName = $useUse->alias ? $useUse->alias->name : $useUse->name->getLast();
$shortName = $useUse->alias !== null ? $useUse->alias->name : $useUse->name->getLast();
$shortNamePattern = preg_quote($shortName, '#');

return (bool) Strings::match($tag, '#' . $shortNamePattern . '(\\\\[\w]+)?#i');
Expand Down
6 changes: 3 additions & 3 deletions packages/BetterPhpDocParser/src/Printer/PhpDocInfoPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private function printNode(
$startEndValueObject = $attributeAwareNode->getAttribute(Attribute::PHP_DOC_NODE_INFO) ?: $startEndValueObject;
$attributeAwareNode = $this->multilineSpaceFormatPreserver->fixMultilineDescriptions($attributeAwareNode);

if ($startEndValueObject) {
if ($startEndValueObject !== null) {
$isLastToken = ($nodeCount === $i);

$output = $this->addTokensFromTo(
Expand All @@ -162,7 +162,7 @@ private function printNode(
}

if ($attributeAwareNode instanceof PhpDocTagNode) {
if ($startEndValueObject) {
if ($startEndValueObject !== null) {
return $this->printPhpDocTagNode($attributeAwareNode, $startEndValueObject, $output);
}

Expand Down Expand Up @@ -246,7 +246,7 @@ private function printPhpDocTagNode(
$phpDocTagNode->value->description
);

if (substr_count($nodeOutput, "\n")) {
if (substr_count($nodeOutput, "\n") !== 0) {
$nodeOutput = Strings::replace($nodeOutput, "#\n#", PHP_EOL . ' * ');
}
}
Expand Down
11 changes: 11 additions & 0 deletions packages/CodeQuality/src/Exception/TooLongException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Rector\CodeQuality\Exception;

use Exception;

final class TooLongException extends Exception
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private function createAnonymousFunction(ClassMethod $classMethod, Node $node):
}

// does method return something?
if ($hasClassMethodReturn) {
if ($hasClassMethodReturn !== []) {
$anonymousFunction->stmts[] = new Return_($innerMethodCall);
} else {
$anonymousFunction->stmts[] = new Expression($innerMethodCall);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PhpParser\NodeTraverser;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -134,7 +135,12 @@ private function resolveFetchedLocalPropertyNameToType(Class_ $class): array

$this->traverseNodesWithCallable($class->stmts, function (Node $node) use (
&$fetchedLocalPropertyNameToTypes
) {
): ?int {
// skip anonymous class scope
if ($this->isAnonymousClass($node)) {
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
}

if (! $node instanceof PropertyFetch) {
return null;
}
Expand Down Expand Up @@ -164,6 +170,8 @@ private function resolveFetchedLocalPropertyNameToType(Class_ $class): array
$propertyFetchType = $this->resolvePropertyFetchType($node);

$fetchedLocalPropertyNameToTypes[$propertyName][] = $propertyFetchType;

return null;
});

// normalize types to union
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Rector\CodeQuality\Rector\Concat;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Scalar\String_;
use Rector\CodeQuality\Exception\TooLongException;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand All @@ -16,6 +18,11 @@
*/
final class JoinStringConcatRector extends AbstractRector
{
/**
* @var int
*/
private const LINE_BREAK_POINT = 80;

public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Joins concat of 2 strings', [
Expand Down Expand Up @@ -56,7 +63,11 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
return $this->joinConcatIfStrings($node);
try {
return $this->joinConcatIfStrings($node);
} catch (TooLongException $tooLongException) {
return null;
}
}

/**
Expand All @@ -80,6 +91,11 @@ private function joinConcatIfStrings(Concat $concat): Node
return $concat;
}

$value = $concat->left->value . $concat->right->value;
if (Strings::length($value) >= self::LINE_BREAK_POINT) {
throw new TooLongException();
}

return new String_($concat->left->value . $concat->right->value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
Expand All @@ -22,6 +23,7 @@

/**
* @see https://phpstan.org/r/e909844a-084e-427e-92ac-fed3c2aeabab
*
* @see \Rector\CodeQuality\Tests\Rector\If_\RemoveAlwaysTrueConditionSetInConstructorRector\RemoveAlwaysTrueConditionSetInConstructorRectorTest
*/
final class RemoveAlwaysTrueConditionSetInConstructorRector extends AbstractRector
Expand Down Expand Up @@ -181,7 +183,26 @@ private function resolvePropertyFetchTypes(PropertyFetch $propertyFetch): array

$resolvedTypes = [];

$this->traverseNodesWithCallable($class->stmts, function (Node $node) use (
// add dfeault vlaue @todo
$defaultValue = $property->props[0]->default;
if ($defaultValue !== null) {
$defaultValueStaticType = $this->getStaticType($defaultValue);
$resolvedTypes[] = $defaultValueStaticType;
}

$assignTypes = $this->resolveAssignTypes($class, $propertyName);

return array_merge($resolvedTypes, $assignTypes);
}

/**
* @return Type[]
*/
private function resolveAssignTypes(ClassLike $classLike, string $propertyName): array
{
$resolvedTypes = [];

$this->traverseNodesWithCallable($classLike->stmts, function (Node $node) use (
$propertyName,
&$resolvedTypes
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private function shortenElseIf(If_ $node): ?Node
// Try to shorten the nested if before transforming it to elseif
$refactored = $this->shortenElseIf($if);

if ($refactored) {
if ($refactored !== null) {
$if = $refactored;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function provideDataForTest(): Iterator
{
yield [__DIR__ . '/Fixture/fixture.php.inc'];
yield [__DIR__ . '/Fixture/multiple_types.php.inc'];
yield [__DIR__ . '/Fixture/skip_anonymous_class.php.inc'];
yield [__DIR__ . '/Fixture/skip_defined.php.inc'];
yield [__DIR__ . '/Fixture/skip_parent_property.php.inc'];
yield [__DIR__ . '/Fixture/skip_trait_used.php.inc'];
Expand Down
Loading