Skip to content

Commit

Permalink
[Php80] Add MixedTypeRector (#2701)
Browse files Browse the repository at this point in the history
* [Php80] Add MixedTypeRector

* fixture

* apply type param

* apply type param

* [ci-review] Rector Rectify

* fix type definition

* [ci-review] Rector Rectify

* identifier

* phpstan

* remove doc only mixed

* doc

* doc & Node

* register

* try check string or object type

* check virtual node

* fix

* update FileCache mixed as skipped in rector.php

* skip no mixed doc param

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* implemented with add MakeClassMethodParamMixedTypedGuard

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* only make typed with use doc @param mixed

* rollback invalid change

* add fixture for override public method

* fixture for skip has pubilc child method

* rename fixture

* void return for paramTagRemover->removeParamTagsIfUseless()

* implemented check has child or parent method 🎉

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Jul 27, 2022
1 parent 4ed677d commit 69d3c92
Show file tree
Hide file tree
Showing 32 changed files with 358 additions and 84 deletions.
2 changes: 2 additions & 0 deletions config/set/php80.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Rector\Php80\Rector\FuncCall\ClassOnObjectRector;
use Rector\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector;
use Rector\Php80\Rector\FuncCall\TokenGetAllToObjectRector;
use Rector\Php80\Rector\FunctionLike\MixedTypeRector;
use Rector\Php80\Rector\FunctionLike\UnionTypesRector;
use Rector\Php80\Rector\Identical\StrEndsWithRector;
use Rector\Php80\Rector\Identical\StrStartsWithRector;
Expand Down Expand Up @@ -105,4 +106,5 @@

$rectorConfig->rule(Php8ResourceReturnToObjectRector::class);
$rectorConfig->rule(AddParamBasedOnParentClassMethodRector::class);
$rectorConfig->rule(MixedTypeRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function removeTagValueFromNode(PhpDocInfo $phpDocInfo, Node $desiredNode
$phpDocNode = $phpDocInfo->getPhpDocNode();

$phpDocNodeTraverser = new PhpDocNodeTraverser();
$phpDocNodeTraverser->traverseWithCallable($phpDocNode, '', static function ($node) use (
$phpDocNodeTraverser->traverseWithCallable($phpDocNode, '', static function (Node $node) use (
$desiredNode,
$phpDocInfo
): ?int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function findByType(PhpDocNode $phpDocNode, string $desiredType): array

$foundNodes = [];

$phpDocNodeTraverser->traverseWithCallable($phpDocNode, '', static function ($node) use (
$phpDocNodeTraverser->traverseWithCallable($phpDocNode, '', static function (Node $node) use (
&$foundNodes,
$desiredType
): Node {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ public function getValue(string | int $key)
return $this->values[$key];
}

/**
* @param mixed $value
*/
public function changeValue(string $key, $value): void
public function changeValue(string $key, mixed $value): void
{
// is quoted?
if (isset($this->values[$key]) && is_string($this->values[$key]) && StringUtils::isMatch(
Expand Down Expand Up @@ -108,10 +105,7 @@ public function getValueWithoutQuotes(string | int $key)
return $this->removeQuotes($value);
}

/**
* @param mixed $value
*/
public function changeSilentValue($value): void
public function changeSilentValue(mixed $value): void
{
// is quoted?
if (StringUtils::isMatch($this->values[0], self::UNQUOTED_VALUE_REGEX)) {
Expand Down Expand Up @@ -232,10 +226,7 @@ protected function printValuesContent(array $values): string
return $itemContents;
}

/**
* @param mixed $value
*/
private function stringifyValue($value): string
private function stringifyValue(mixed $value): string
{
// @todo resolve original casing
if ($value === false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ public function __toString(): string
return $this->implode($this->values);
}

/**
* @param mixed $value
*/
private function stringifyValue($value): string
private function stringifyValue(mixed $value): string
{
if ($value === false) {
return 'false';
Expand Down
5 changes: 1 addition & 4 deletions packages/Caching/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ public function load(string $key, string $variableKey)
return $this->cacheStorage->load($key, $variableKey);
}

/**
* @param mixed $data
*/
public function save(string $key, string $variableKey, $data): void
public function save(string $key, string $variableKey, mixed $data): void
{
$this->cacheStorage->save($key, $variableKey, $data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ interface CacheStorageInterface
*/
public function load(string $key, string $variableKey);

/**
* @param mixed $data
*/
public function save(string $key, string $variableKey, $data): void;
public function save(string $key, string $variableKey, mixed $data): void;

public function clean(string $key): void;

Expand Down
5 changes: 1 addition & 4 deletions packages/Caching/ValueObject/CacheItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@
*/
final class CacheItem
{
/**
* @param mixed $data
*/
public function __construct(
private readonly string $variableKey,
private $data
private readonly mixed $data
) {
}

Expand Down
2 changes: 1 addition & 1 deletion packages/Caching/ValueObject/Storage/FileCacheStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function load(string $key, string $variableKey)
})($key, $variableKey);
}

public function save(string $key, string $variableKey, $data): void
public function save(string $key, string $variableKey, mixed $data): void
{
$cacheFilePaths = $this->getCacheFilePaths($key);
$this->smartFileSystem->mkdir($cacheFilePaths->getFirstDirectory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function load(string $key, string $variableKey)
return $item->getData();
}

public function save(string $key, string $variableKey, $data): void
public function save(string $key, string $variableKey, mixed $data): void
{
$this->storage[$key] = new CacheItem($variableKey, $data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@ private function shouldSkipNullItems(Array_ $array): bool
return $array->items[1] === null;
}

/**
* @param mixed $values
*/
private function shouldSkipAssociativeArray($values): bool
private function shouldSkipAssociativeArray(mixed $values): bool
{
if (! is_array($values)) {
return false;
Expand Down
4 changes: 2 additions & 2 deletions packages/NodeNestingScope/ScopeNestingComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function isInBothIfElseBranch(Node $foundParentNode, Expr $seekedExpr): b

$foundIfNode = (bool) $this->betterNodeFinder->findFirst(
$foundParentNode->stmts,
fn ($node): bool => $this->nodeComparator->areNodesEqual($node, $seekedExpr)
fn (Node $node): bool => $this->nodeComparator->areNodesEqual($node, $seekedExpr)
);

if ($foundParentNode->else === null) {
Expand All @@ -93,7 +93,7 @@ public function isInBothIfElseBranch(Node $foundParentNode, Expr $seekedExpr): b

$foundElseNode = (bool) $this->betterNodeFinder->findFirst(
$foundParentNode->else,
fn ($node): bool => $this->nodeComparator->areNodesEqual($node, $seekedExpr)
fn (Node $node): bool => $this->nodeComparator->areNodesEqual($node, $seekedExpr)
);

if ($foundIfNode && $foundElseNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ public function map($value): Expr
return new Array_($arrayItems);
}

/**
* @param mixed $value
*/
private function isRemoveArrayPlaceholder($value): bool
private function isRemoveArrayPlaceholder(mixed $value): bool
{
if (! is_array($value)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Rector\Tests\Php80\Rector\FunctionLike\MixedTypeRector\Fixture;

class Fixture
{
/**
* @param mixed $param
*/
public function run($param)
{
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\FunctionLike\MixedTypeRector\Fixture;

class Fixture
{
public function run(mixed $param)
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Rector\Tests\Php80\Rector\FunctionLike\MixedTypeRector\Fixture;

class RemoveMixedParamDoc
{
/**
* @param mixed $param
*/
public function run(mixed $param)
{
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\FunctionLike\MixedTypeRector\Fixture;

class RemoveMixedParamDoc
{
public function run(mixed $param)
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Rector\Tests\Php80\Rector\FunctionLike\MixedTypeRector\Fixture;

class SkipAlreadyMixedType
{
public function run(mixed $param)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rector\Tests\Php80\Rector\FunctionLike\MixedTypeRector\Fixture;

class SkipHasChildMethod
{
/**
* @param mixed $param
*/
public function run($param)
{
}
}

class SomeChild extends SkipHasChildMethod
{
/**
* @param int $param
*/
public function run($param)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Rector\Tests\Php80\Rector\FunctionLike\MixedTypeRector\Fixture;

class NoMixedParamDoc
{
public function run($param)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\Php80\Rector\FunctionLike\MixedTypeRector\Fixture;

use Rector\Tests\Php80\Rector\FunctionLike\MixedTypeRector\Source\SomeParentNotMixedDocParam;

class SkipOverrideParentMethod extends SomeParentNotMixedDocParam
{
/**
* @param mixed $param
*/
public function run($param)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php80\Rector\FunctionLike\MixedTypeRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class MixedTypeRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Php80\Rector\FunctionLike\MixedTypeRector\Source;

class SomeParentNotMixedDocParam
{
/**
* @param int $param
*/
public function run($param)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\Php80\Rector\FunctionLike\MixedTypeRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(MixedTypeRector::class);

$rectorConfig->phpVersion(PhpVersionFeature::MIXED_TYPE);
};
15 changes: 3 additions & 12 deletions rules/Arguments/ArgumentDefaultValueReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ public function processReplaces(
return $this->processArgs($node, $replaceArgumentDefaultValue);
}

/**
* @param mixed $value
*/
public function isDefaultValueMatched(?Expr $expr, $value): bool
public function isDefaultValueMatched(?Expr $expr, mixed $value): bool
{
// allow any values before, also allow param without default value
if ($value === ReplaceArgumentDefaultValue::ANY_VALUE_BEFORE) {
Expand Down Expand Up @@ -110,18 +107,12 @@ private function processArgs(
return $expr;
}

/**
* @param mixed $value
*/
private function normalizeValueToArgument($value): Arg
private function normalizeValueToArgument(mixed $value): Arg
{
return new Arg($this->normalizeValue($value));
}

/**
* @param mixed $value
*/
private function normalizeValue($value): ClassConstFetch|Expr
private function normalizeValue(mixed $value): ClassConstFetch|Expr
{
// class constants → turn string to composite
if (is_string($value) && \str_contains($value, '::')) {
Expand Down

0 comments on commit 69d3c92

Please sign in to comment.