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
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,6 @@ parameters:
- '#Method Rector\\Caching\\ChangedFilesDetector\:\:hashFile\(\) should return string but returns string\|false#'

- '#If condition is always false#'
- '#Method Rector\\Php80\\Rector\\Identical\\StrStartsWithRector\:\:refactor\(\) should return PhpParser\\Node\|null but return statement is missing#'

- '#Parameter \#1 \$funcCall of method Rector\\Php80\\MatchAndRefactor\\StrStartsWithMatchAndRefactor\\AbstractMatchAndRefactor\:\:createStrStartsWithValueObjectFromFuncCall\(\) expects PhpParser\\Node\\Expr\\FuncCall, PhpParser\\Node\\Expr given#'
21 changes: 21 additions & 0 deletions rules/php80/src/Contract/StrStartWithMatchAndRefactorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Rector\Php80\Contract;

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\FuncCall;
use Rector\Php80\ValueObject\StrStartsWithValueObject;

interface StrStartWithMatchAndRefactorInterface
{
public function match(BinaryOp $binaryOp): ?StrStartsWithValueObject;

/**
* @return FuncCall|BooleanNot|null
*/
public function refactor(StrStartsWithValueObject $strStartsWithValueObject): ?Node;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
namespace Rector\Php80\MatchAndRefactor\StrStartsWithMatchAndRefactor;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use Rector\Core\PhpParser\Node\Value\ValueResolver;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\Php80\ValueObject\StrStartsWithValueObject;

abstract class AbstractMatchAndRefactor
{
Expand Down Expand Up @@ -48,4 +52,32 @@ protected function isFuncCallName(Node $node, string $name): bool

return $this->nodeNameResolver->isName($node, $name);
}

/**
* @return FuncCall|BooleanNot
*/
protected function createStrStartsWith(StrStartsWithValueObject $strStartsWithValueObject): Node
{
$args = [
new Arg($strStartsWithValueObject->getHaystackExpr()),
new Arg($strStartsWithValueObject->getNeedleExpr()),
];

$funcCall = new FuncCall(new Name('str_starts_with'), $args);
if ($strStartsWithValueObject->isPositive()) {
return $funcCall;
}

return new BooleanNot($funcCall);
}

protected function createStrStartsWithValueObjectFromFuncCall(
FuncCall $funcCall,
bool $isPositive
): StrStartsWithValueObject {
$haystack = $funcCall->args[0]->value;
$needle = $funcCall->args[1]->value;

return new StrStartsWithValueObject($funcCall, $haystack, $needle, $isPositive);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@
namespace Rector\Php80\MatchAndRefactor\StrStartsWithMatchAndRefactor;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use Rector\Php80\ValueObject\StrncmpFuncCallToHaystack;
use Rector\Php80\Contract\StrStartWithMatchAndRefactorInterface;
use Rector\Php80\ValueObject\StrStartsWithValueObject;

final class StrncmpMatchAndRefactor extends AbstractMatchAndRefactor
final class StrncmpMatchAndRefactor extends AbstractMatchAndRefactor implements StrStartWithMatchAndRefactorInterface
{
/**
* @var string
Expand All @@ -25,30 +22,25 @@ final class StrncmpMatchAndRefactor extends AbstractMatchAndRefactor
/**
* @param Identical|NotIdentical $binaryOp
*/
public function match(BinaryOp $binaryOp): ?StrncmpFuncCallToHaystack
public function match(BinaryOp $binaryOp): ?StrStartsWithValueObject
{
$isPositive = $binaryOp instanceof Identical;

if ($this->isFuncCallName($binaryOp->left, self::FUNCTION_NAME)) {
/** @var FuncCall $funcCall */
$funcCall = $binaryOp->left;
return new StrncmpFuncCallToHaystack($funcCall, $isPositive);
return $this->createStrStartsWithValueObjectFromFuncCall($binaryOp->left, $isPositive);
}

if ($this->isFuncCallName($binaryOp->right, self::FUNCTION_NAME)) {
/** @var FuncCall $funcCall */
$funcCall = $binaryOp->right;
return new StrncmpFuncCallToHaystack($funcCall, $isPositive);
return $this->createStrStartsWithValueObjectFromFuncCall($binaryOp->right, $isPositive);
}

return null;
}

public function refactor(StrncmpFuncCallToHaystack $strncmpFuncCallToHaystack): ?Node
public function refactor(StrStartsWithValueObject $strStartsWithValueObject): ?Node
{
$strncmpFuncCall = $strncmpFuncCallToHaystack->getStrncmpFuncCall();

$needleExpr = $strncmpFuncCall->args[1]->value;
$strncmpFuncCall = $strStartsWithValueObject->getFuncCall();
$needleExpr = $strStartsWithValueObject->getNeedleExpr();

if (! $this->isFuncCallName($strncmpFuncCall->args[2]->value, 'strlen')) {
return null;
Expand All @@ -62,19 +54,6 @@ public function refactor(StrncmpFuncCallToHaystack $strncmpFuncCallToHaystack):
return null;
}

$strStartsWith = $this->createStrStartsWith($strncmpFuncCall, $needleExpr);
if ($strncmpFuncCallToHaystack->isPositive()) {
return $strStartsWith;
}

return new BooleanNot($strStartsWith);
}

private function createStrStartsWith(FuncCall $strncmpFuncCall, Expr $needleExpr): FuncCall
{
$haystackExpr = $strncmpFuncCall->args[0]->value;
$args = [new Arg($haystackExpr), new Arg($needleExpr)];

return new FuncCall(new Name('str_starts_with'), $args);
return $this->createStrStartsWith($strStartsWithValueObject);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,35 @@

namespace Rector\Php80\MatchAndRefactor\StrStartsWithMatchAndRefactor;

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use Rector\Php80\ValueObject\StrposFuncCallToZero;
use Rector\Php80\Contract\StrStartWithMatchAndRefactorInterface;
use Rector\Php80\ValueObject\StrStartsWithValueObject;

final class StrposMatchAndRefactor extends AbstractMatchAndRefactor
final class StrposMatchAndRefactor extends AbstractMatchAndRefactor implements StrStartWithMatchAndRefactorInterface
{
/**
* @param Identical|NotIdentical $binaryOp
*/
public function match(BinaryOp $binaryOp): ?StrposFuncCallToZero
public function match(BinaryOp $binaryOp): ?StrStartsWithValueObject
{
$isPositive = $binaryOp instanceof Identical;

if ($this->isFuncCallName($binaryOp->left, 'strpos')) {
if (! $this->valueResolver->isValue($binaryOp->right, 0)) {
return null;
}

/** @var FuncCall $funcCall */
$funcCall = $binaryOp->left;
return new StrposFuncCallToZero($funcCall);
$haystack = $funcCall->args[0]->value;
$needle = $funcCall->args[1]->value;

return new StrStartsWithValueObject($funcCall, $haystack, $needle, $isPositive);
}

if ($this->isFuncCallName($binaryOp->right, 'strpos')) {
Expand All @@ -35,16 +42,23 @@ public function match(BinaryOp $binaryOp): ?StrposFuncCallToZero

/** @var FuncCall $funcCall */
$funcCall = $binaryOp->right;
return new StrposFuncCallToZero($funcCall);
$haystack = $funcCall->args[0]->value;
$needle = $funcCall->args[1]->value;

return new StrStartsWithValueObject($funcCall, $haystack, $needle, $isPositive);
}

return null;
}

public function refactor(StrposFuncCallToZero $strposFuncCallToZero): FuncCall
/**
* @return FuncCall
*/
public function refactor(StrStartsWithValueObject $StrStartsWithValueObject): ?Node
{
$strposFuncCall = $strposFuncCallToZero->getStrposFuncCall();
$strposFuncCall = $StrStartsWithValueObject->getFuncCall();
$strposFuncCall->name = new Name('str_starts_with');

return $strposFuncCall;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,44 @@
namespace Rector\Php80\MatchAndRefactor\StrStartsWithMatchAndRefactor;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use Rector\Php80\ValueObject\SubstrFuncCallToHaystack;
use Rector\Php80\Contract\StrStartWithMatchAndRefactorInterface;
use Rector\Php80\ValueObject\StrStartsWithValueObject;

final class SubstrMatchAndRefactor extends AbstractMatchAndRefactor
final class SubstrMatchAndRefactor extends AbstractMatchAndRefactor implements StrStartWithMatchAndRefactorInterface
{
/**
* @param Identical|NotIdentical $binaryOp
*/
public function match(BinaryOp $binaryOp): ?SubstrFuncCallToHaystack
public function match(BinaryOp $binaryOp): ?StrStartsWithValueObject
{
$isPositive = $binaryOp instanceof Identical;

if ($this->isFuncCallName($binaryOp->left, 'substr')) {
/** @var FuncCall $funcCall */
$funcCall = $binaryOp->left;
return new SubstrFuncCallToHaystack($funcCall, $binaryOp->right, $isPositive);
$haystack = $funcCall->args[0]->value;

return new StrStartsWithValueObject($funcCall, $haystack, $binaryOp->right, $isPositive);
}

if ($this->isFuncCallName($binaryOp->right, 'substr')) {
/** @var FuncCall $funcCall */
$funcCall = $binaryOp->right;
return new SubstrFuncCallToHaystack($funcCall, $binaryOp->left, $isPositive);
$haystack = $funcCall->args[0]->value;

return new StrStartsWithValueObject($funcCall, $haystack, $binaryOp->left, $isPositive);
}

return null;
}

public function refactor(SubstrFuncCallToHaystack $substrFuncCallToHaystack): ?Node
public function refactor(StrStartsWithValueObject $strStartsWithValueObject): ?Node
{
$substrFuncCall = $substrFuncCallToHaystack->getSubstrFuncCall();
$substrFuncCall = $strStartsWithValueObject->getFuncCall();
if (! $this->valueResolver->isValue($substrFuncCall->args[1]->value, 0)) {
return null;
}
Expand All @@ -54,25 +55,11 @@ public function refactor(SubstrFuncCallToHaystack $substrFuncCallToHaystack): ?N
$strlenFuncCall = $substrFuncCall->args[2]->value;
$needleExpr = $strlenFuncCall->args[0]->value;

$comparedExpr = $substrFuncCallToHaystack->getHaystackExpr();
if (! $this->betterStandardPrinter->areNodesEqual($needleExpr, $comparedExpr)) {
$comparedNeedleExpr = $strStartsWithValueObject->getNeedleExpr();
if (! $this->betterStandardPrinter->areNodesEqual($needleExpr, $comparedNeedleExpr)) {
return null;
}

$strStartsWith = $this->createStrStartsWith($substrFuncCall, $needleExpr);
if ($substrFuncCallToHaystack->isPositive()) {
return $strStartsWith;
}

return new BooleanNot($strStartsWith);
}

private function createStrStartsWith(FuncCall $substrFuncCall, Expr $needleExpr): FuncCall
{
$haystackExpr = $substrFuncCall->args[0]->value;

$args = [new Arg($haystackExpr), new Arg($needleExpr)];

return new FuncCall(new Name('str_starts_with'), $args);
return $this->createStrStartsWith($strStartsWithValueObject);
}
}
51 changes: 13 additions & 38 deletions rules/php80/src/Rector/Identical/StrStartsWithRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\Php80\MatchAndRefactor\StrStartsWithMatchAndRefactor\StrncmpMatchAndRefactor;
use Rector\Php80\MatchAndRefactor\StrStartsWithMatchAndRefactor\StrposMatchAndRefactor;
use Rector\Php80\MatchAndRefactor\StrStartsWithMatchAndRefactor\SubstrMatchAndRefactor;
use Rector\Php80\Contract\StrStartWithMatchAndRefactorInterface;

/**
* @see https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions
Expand All @@ -25,28 +23,16 @@
final class StrStartsWithRector extends AbstractRector
{
/**
* @var StrncmpMatchAndRefactor
* @var StrStartWithMatchAndRefactorInterface[]
*/
private $strncmpMatchAndRefactor;
private $strStartWithMatchAndRefactors = [];

/**
* @var StrposMatchAndRefactor
* @param StrStartWithMatchAndRefactorInterface[] $strStartWithMatchAndRefactors
*/
private $strposMatchAndRefactor;

/**
* @var SubstrMatchAndRefactor
*/
private $substrMatchAndRefactor;

public function __construct(
StrncmpMatchAndRefactor $strncmpMatchAndRefactor,
StrposMatchAndRefactor $strposMatchAndRefactor,
SubstrMatchAndRefactor $substrMatchAndRefactor
) {
$this->strncmpMatchAndRefactor = $strncmpMatchAndRefactor;
$this->strposMatchAndRefactor = $strposMatchAndRefactor;
$this->substrMatchAndRefactor = $substrMatchAndRefactor;
public function __construct(array $strStartWithMatchAndRefactors)
{
$this->strStartWithMatchAndRefactors = $strStartWithMatchAndRefactors;
}

public function getDefinition(): RectorDefinition
Expand Down Expand Up @@ -94,24 +80,13 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
// 1. substr
$substrFuncCallToHaystack = $this->substrMatchAndRefactor->match($node);
if ($substrFuncCallToHaystack !== null) {
return $this->substrMatchAndRefactor->refactor($substrFuncCallToHaystack);
}

// 2. strpos
$strposFuncCallToZero = $this->strposMatchAndRefactor->match($node);
if ($strposFuncCallToZero !== null) {
return $this->strposMatchAndRefactor->refactor($strposFuncCallToZero);
}
foreach ($this->strStartWithMatchAndRefactors as $strStartWithMatchAndRefactor) {
$strStartsWithValueObject = $strStartWithMatchAndRefactor->match($node);
if ($strStartsWithValueObject === null) {
continue;
}

// 3. strcmp
$strcmpFuncCallToHaystack = $this->strncmpMatchAndRefactor->match($node);
if ($strcmpFuncCallToHaystack !== null) {
return $this->strncmpMatchAndRefactor->refactor($strcmpFuncCallToHaystack);
return $strStartWithMatchAndRefactor->refactor($strStartsWithValueObject);
}

return $node;
}
}
Loading