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
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public function __toString(): string
$variadic = $this->isVariadic ? '...' : '';
$reference = $this->isReference ? '&' : '';

return trim("{$this->type} {$variadic}{$reference}{$this->parameterName} {$this->description}");
return trim(
sprintf('%s %s%s%s %s', $this->type, $variadic, $reference, $this->parameterName, $this->description)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ function ($token): string {
);
$table = implode('/', $table);

return new String_("{$prefix}.{$table}", $name->getAttributes());
return new String_(sprintf('%s.%s', $prefix, $table), $name->getAttributes());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private function createNewProperties(array $fetchedLocalPropertyNameToTypes, arr

if ($this->isAtLeastPhpVersion('7.4')) {
$phpStanNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($propertyType);
if ($phpStanNode) {
if ($phpStanNode !== null) {
$property->type = $phpStanNode;
} else {
// fallback to doc type in PHP 7.4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public function refactor(Node $node): ?Node
return $node;
}

$firstArgumentValue = $node->args[0]->value;
if ($firstArgumentValue instanceof String_) {
return null;
}

if (count($node->args) === 2) {
if ($this->isStringOrUnionStringOnlyType($node->args[1]->value)) {
$node->args = array_reverse($node->args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Expr\BooleanNot;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\PhpParser\Node\Manipulator\BinaryOpManipulator;
Expand Down Expand Up @@ -70,7 +71,14 @@ function (Node $node): bool {
/** @var Expr $comparedNode */
[$comparedNode, ] = $matchedNodes;

if ($this->hasNullOrIntegerType($this->getStaticType($comparedNode))) {
$staticType = $this->getStaticType($comparedNode);

// nothing we can do
if ($staticType instanceof MixedType) {
return null;
}

if ($this->hasNullOrIntegerType($staticType)) {
return null;
}

Expand All @@ -85,15 +93,16 @@ function (Node $node): bool {
* E.g strpos() can return 0 and false, so this would be false positive:
* ! 0 → true
* ! false → true
* ! mixed → true/false
*/
private function hasNullOrIntegerType(?Type $staticType): bool
{
if ($staticType instanceof UnionType) {
return $staticType->isSuperTypeOf(new IntegerType())->yes() && $staticType->isSuperTypeOf(
new ConstantBooleanType(false)
)->yes();
if (! $staticType instanceof UnionType) {
return false;
}

return false;
return $staticType->isSuperTypeOf(new IntegerType())->yes() && $staticType->isSuperTypeOf(
new ConstantBooleanType(false)
)->yes();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Stmt\If_;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
Expand All @@ -33,7 +34,7 @@ public function getDefinition(): RectorDefinition
new CodeSample(
<<<'PHP'
/** @var stdClass|null $value */
if ($value) {
if ($value) {
}

if (!$value) {
Expand Down Expand Up @@ -113,6 +114,12 @@ private function isNullableNonScalarType(Node $node): bool
if ($staticType->isSuperTypeOf(new IntegerType())->yes()) {
return false;
}

// is bool?
if ($staticType->isSuperTypeOf(new BooleanType())->yes()) {
return false;
}

return ! $staticType->isSuperTypeOf(new FloatType())->yes();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function test(string $file): void
public function provideDataForTest(): Iterator
{
yield [__DIR__ . '/Fixture/fixture.php.inc'];
yield [__DIR__ . '/Fixture/skip_already_switched.php.inc'];
}

protected function getRectorClass(): string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rector\CodingStyle\Tests\Rector\FuncCall\ConsistentImplodeRector\Fixture;

class SkipAlreadySwitched
{
/**
* @param string[] $versionedSets
*/
public function createSetListInString(array $versionedSets): string
{
$setsListInString = '';

foreach ($versionedSets as $groupName => $configName) {
$setsListInString .= ' * ' . $groupName . ': ' . implode(', ', $configName) . PHP_EOL;
}

return $setsListInString;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
<?php

function identicalFalseToBooleanNot()
function identicalFalseToBooleanNot($something)
{
if ($something === false) {
}

if (false === $something) {
}

if (!$something === false) {
}

Expand All @@ -20,14 +14,8 @@ function identicalFalseToBooleanNot()
-----
<?php

function identicalFalseToBooleanNot()
function identicalFalseToBooleanNot($something)
{
if (!$something) {
}

if (!$something) {
}

if ($something) {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rector\CodingStyle\Tests\Rector\Identical\IdenticalFalseToBooleanNotRector\Fixture;

final class SkipMixed
{
/**
* @param mixed $attribute
*/
public function run($attribute)
{
if ($attribute === null) {
return 'null';
} elseif ($attribute === false) {
return 'false';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public function test(string $file): void

public function provideDataForTest(): Iterator
{
yield [__DIR__ . '/Fixture/fixture.php.inc'];
yield [__DIR__ . '/Fixture/skip_null_false.php.inc'];
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

protected function getRectorClass(): string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\CodingStyle\Tests\Rector\If_\NullableCompareToNullRector\Fixture;

class KeepNullableBool
{
public function run(?bool $boolOrNull)
{
if (! $boolOrNull) {
return 'no item nor null';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ public function test(string $file): void

public function provideDataForTest(): Iterator
{
yield [__DIR__ . '/Fixture/fixture.php.inc'];
yield [__DIR__ . '/Fixture/fixture2.php.inc'];
yield [__DIR__ . '/Fixture/fixture3.php.inc'];
yield [__DIR__ . '/Fixture/nullable_scalars.php.inc'];
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

protected function getRectorClass(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ private function isParentClassMethodVisibilityOverride(ClassMethod $classMethod,
$className = $staticCall->getAttribute(AttributeKey::CLASS_NAME);

$parentClassName = get_parent_class($className);
if ($parentClassName === false) {
if (! $parentClassName) {
throw new ShouldNotHappenException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function test(string $file): void

public function provideDataForTest(): Iterator
{
yield from $this->provideEachFileInDir(__DIR__ . '/Fixture');
yield from $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

protected function getRectorClass(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function createTemporaryUuidProperty(): Property
{
$uuidPropertyBuilder = $this->builderFactory->property('uuid');
$uuidPropertyBuilder->makePrivate();

$uuidProperty = $uuidPropertyBuilder->getNode();

$this->decoratePropertyWithUuidAnnotations($uuidProperty, true, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function refactor(Node $node): ?Node
}

// is already set?
if ($node->returnType) {
if ($node->returnType !== null) {
$currentType = $this->getName($node->returnType);
if ($currentType === UuidInterface::class) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function refactor(Node $node): ?Node
$this->renameUuidVariableToId($node);

// is already set?
if ($node->params[0]->type) {
if ($node->params[0]->type !== null) {
$currentType = $this->getName($node->params[0]->type);
if ($currentType === UuidInterface::class) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private function findClassMethodByParsingReflection(Node $node): ?ClassMethod
}

$fileName = $reflectionMethod->getFileName();
if ($fileName === false || ! file_exists($fileName)) {
if (! $fileName || ! file_exists($fileName)) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/RemovingStatic/src/UniqueObjectFactoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private function createConstructMethod(ObjectType $objectType): ClassMethod
$paramBuilder = $this->builderFactory->param($propertyName);

$typeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($objectType);
if ($typeNode) {
if ($typeNode !== null) {
$paramBuilder->setType($typeNode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class ResponseStatusCodeRector extends AbstractRector
private const RESPONSE_CLASS = 'Symfony\Component\HttpFoundation\Response';

/**
* @var array
* @var string[]
*/
private const CODE_TO_CONST = [
100 => 'HTTP_CONTINUE',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function refactor(Node $node): ?Node

private function refactorReturnTypeDeclaration(ClassMethod $classMethod): void
{
if ($classMethod->returnType) {
if ($classMethod->returnType !== null) {
// already set
if ($this->isName($classMethod->returnType, 'int')) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,12 @@ private function addBuildFormMethod(Class_ $classNode, ClassMethod $classMethod)

$formBuilderParamBuilder = $this->builderFactory->param('builder');
$formBuilderParamBuilder->setType(new FullyQualified(FormBuilderInterface::class));

$formBuilderParam = $formBuilderParamBuilder->getNode();

$optionsParamBuilder = $this->builderFactory->param('options');
$optionsParamBuilder->setType('array');

$optionsParam = $optionsParamBuilder->getNode();

$buildFormClassMethodBuilder = $this->builderFactory->method('buildForm');
Expand Down Expand Up @@ -266,6 +268,7 @@ private function addConfigureOptionsMethod(Class_ $classNode, array $namesToArgs

$resolverParamBuilder = $this->builderFactory->param('resolver');
$resolverParamBuilder->setType(new FullyQualified(OptionsResolver::class));

$resolverParam = $resolverParamBuilder->getNode();

$optionsDefaults = new Array_();
Expand All @@ -282,6 +285,7 @@ private function addConfigureOptionsMethod(Class_ $classNode, array $namesToArgs
$configureOptionsClassMethodBuilder->makePublic();
$configureOptionsClassMethodBuilder->addParam($resolverParam);
$configureOptionsClassMethodBuilder->addStmt($setDefaultsMethodCall);

$configureOptionsClassMethod = $configureOptionsClassMethodBuilder->getNode();

$classNode->stmts[] = $configureOptionsClassMethod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function createSetUpClassMethodWithGetTypes(Class_ $class, array $service
$classMethodBuilder = $this->builderFactory->method('setUp');
$classMethodBuilder->makeProtected();
$classMethodBuilder->addStmts($stmts);

$classMethod = $classMethodBuilder->getNode();

$this->phpUnitTypeDeclarationDecorator->decorate($classMethod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->returnType) {
if ($node->returnType !== null) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function refactor(Node $node): ?Node
private function shouldSkip(Node $node): bool
{
if (! $this->overrideExistingReturnTypes) {
if ($node->returnType) {
if ($node->returnType !== null) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function inferProperty(Property $property): Type
}

// A. infer from type declaration of parameter
if ($param->type) {
if ($param->type !== null) {
$type = $this->resolveParamTypeToPHPStanType($param);
if ($type instanceof MixedType) {
return new MixedType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,7 @@ public function test(string $file): void

public function provideDataForTest(): Iterator
{
yield [__DIR__ . '/Fixture/constructor_param.php.inc'];
yield [__DIR__ . '/Fixture/constructor_param_with_aliased_param.php.inc'];
yield [__DIR__ . '/Fixture/complex.php.inc'];
yield [__DIR__ . '/Fixture/single_nullable_return.php.inc'];
yield [__DIR__ . '/Fixture/getter_type.php.inc'];
yield [__DIR__ . '/Fixture/getter_type_from_var_doc.php.inc'];
yield [__DIR__ . '/Fixture/constructor_param_with_nullable.php.inc'];
yield [__DIR__ . '/Fixture/constructor_array_param_with_nullable.php.inc'];
yield [__DIR__ . '/Fixture/constructor_assign.php.inc'];
yield [__DIR__ . '/Fixture/phpunit_setup.php.inc'];
yield [__DIR__ . '/Fixture/default_value.php.inc'];
yield [__DIR__ . '/Fixture/doctrine_column.php.inc'];
yield [__DIR__ . '/Fixture/doctrine_relation_to_many.php.inc'];
yield [__DIR__ . '/Fixture/doctrine_relation_to_one.php.inc'];
yield [__DIR__ . '/Fixture/doctrine_relation_target_entity_same_namespace.php.inc'];
yield [__DIR__ . '/Fixture/setter_type.php.inc'];
yield [__DIR__ . '/Fixture/skip_multi_vars.php.inc'];
yield [__DIR__ . '/Fixture/skip_anonymous_class.php.inc'];
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

protected function getRectorClass(): string
Expand Down
Loading