diff --git a/config/set/phpoffice/phpexcel-to-phpspreadsheet.yaml b/config/set/php-office/phpexcel-to-phpspreadsheet.yaml similarity index 99% rename from config/set/phpoffice/phpexcel-to-phpspreadsheet.yaml rename to config/set/php-office/phpexcel-to-phpspreadsheet.yaml index 68220cdb429a..5e59802054fd 100644 --- a/config/set/phpoffice/phpexcel-to-phpspreadsheet.yaml +++ b/config/set/php-office/phpexcel-to-phpspreadsheet.yaml @@ -16,6 +16,7 @@ services: Rector\PHPOffice\Rector\MethodCall\RemoveSetTempDirOnExcelWriterRector: null Rector\PHPOffice\Rector\MethodCall\ChangeDuplicateStyleArrayToApplyFromArrayRector: null Rector\PHPOffice\Rector\MethodCall\GetDefaultStyleToGetParentRector: null + Rector\PHPOffice\Rector\MethodCall\IncreaseColumnIndexRector: null Rector\Renaming\Rector\MethodCall\RenameMethodCallRector: $oldToNewMethodsByClass: diff --git a/docs/AllRectorsOverview.md b/docs/AllRectorsOverview.md index b725182f8fde..5a7f11d1740c 100644 --- a/docs/AllRectorsOverview.md +++ b/docs/AllRectorsOverview.md @@ -1,4 +1,4 @@ -# All 496 Rectors Overview +# All 497 Rectors Overview - [Projects](#projects) - [General](#general) @@ -5328,6 +5328,27 @@ Methods to (new Worksheet())->getDefaultStyle() to getParent()->getDefaultStyle(
+### `IncreaseColumnIndexRector` + +- class: [`Rector\PHPOffice\Rector\MethodCall\IncreaseColumnIndexRector`](/../master/rules/php-office/src/Rector/MethodCall/IncreaseColumnIndexRector.php) +- [test fixtures](/../master/rules/php-office/tests/Rector/MethodCall/IncreaseColumnIndexRector/Fixture) + +Column index changed from 0 to 1 - run only ONCE! changes current value without memory + +```diff + final class SomeClass + { + public function run(): void + { + $worksheet = new \PHPExcel_Worksheet(); +- $worksheet->setCellValueByColumnAndRow(0, 3, '1150'); ++ $worksheet->setCellValueByColumnAndRow(1, 3, '1150'); + } + } +``` + +
+ ### `RemoveSetTempDirOnExcelWriterRector` - class: [`Rector\PHPOffice\Rector\MethodCall\RemoveSetTempDirOnExcelWriterRector`](/../master/rules/php-office/src/Rector/MethodCall/RemoveSetTempDirOnExcelWriterRector.php) diff --git a/helpers/extract_default_value_changes_from_diff.php b/helpers/extract_default_value_changes_from_diff.php index bd72e13f5684..a3ab8f2de540 100644 --- a/helpers/extract_default_value_changes_from_diff.php +++ b/helpers/extract_default_value_changes_from_diff.php @@ -51,7 +51,7 @@ final class RemovedDefaultValueDiffExtractor public function __construct() { - $yaml = Yaml::parseFile(__DIR__ . '/../config/set/phpoffice/phpexcel-to-phpspreadsheet.yaml'); + $yaml = Yaml::parseFile(__DIR__ . '/../config/set/php-office/phpexcel-to-phpspreadsheet.yaml'); $oldToNewClasses = $yaml['services']['Rector\Renaming\Rector\Class_\RenameClassRector']['$oldToNewClasses']; $this->newToOldClasses = array_flip($oldToNewClasses); diff --git a/phpstan.neon b/phpstan.neon index c589efe36f81..64f21de6836f 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -260,3 +260,4 @@ parameters: - '#Cannot call method getParentNode\(\) on Rector\\DeadCode\\ValueObject\\VariableNodeUse\|null#' - '#Method Rector\\DeadCode\\NodeFinder\\PreviousVariableAssignNodeFinder\:\:find\(\) should return PhpParser\\Node\\Expr\\Assign\|null but returns PhpParser\\Node\|null#' - '#Parameter \#2 \$name of method Rector\\NodeNameResolver\\NodeNameResolver\:\:isName\(\) expects string, string\|null given#' + - '#Method Rector\\PHPOffice\\Rector\\MethodCall\\IncreaseColumnIndexRector\:\:findVariableAssignName\(\) should return PhpParser\\Node\\Expr\\Assign\|null but returns PhpParser\\Node\|null#' diff --git a/rules/php-office/src/Rector/MethodCall/IncreaseColumnIndexRector.php b/rules/php-office/src/Rector/MethodCall/IncreaseColumnIndexRector.php new file mode 100644 index 000000000000..55d38dabcfea --- /dev/null +++ b/rules/php-office/src/Rector/MethodCall/IncreaseColumnIndexRector.php @@ -0,0 +1,167 @@ +setCellValueByColumnAndRow(0, 3, '1150'); + } +} +PHP +, + <<<'PHP' +final class SomeClass +{ + public function run(): void + { + $worksheet = new \PHPExcel_Worksheet(); + $worksheet->setCellValueByColumnAndRow(1, 3, '1150'); + } +} +PHP + + ), + ] + ); + } + + /** + * @return string[] + */ + public function getNodeTypes(): array + { + return [MethodCall::class]; + } + + /** + * @param MethodCall $node + */ + public function refactor(Node $node): ?Node + { + if (! $this->isObjectType($node->var, 'PHPExcel_Worksheet')) { + return null; + } + + if (! $this->isName($node->name, '*ByColumnAndRow')) { + return null; + } + + // increase column value + $firstArumentValue = $node->args[0]->value; + if ($firstArumentValue instanceof LNumber) { + ++$firstArumentValue->value; + } + + if ($firstArumentValue instanceof BinaryOp) { + $this->refactorBinaryOp($firstArumentValue); + } + + if ($firstArumentValue instanceof Variable) { + $parentNode = $this->getParentNode($node); + + if (! $parentNode instanceof For_) { + $node->args[0]->value = new Plus($firstArumentValue, new LNumber(1)); + return null; + } + + // check if for() value, rather update that + $this->refactorFor($firstArumentValue, $parentNode); + } + + return $node; + } + + private function refactorBinaryOp(BinaryOp $binaryOp): void + { + if ($binaryOp->left instanceof LNumber) { + ++$binaryOp->left->value; + return; + } + + if ($binaryOp->right instanceof LNumber) { + ++$binaryOp->right->value; + return; + } + } + + /** + * @param Node|Node[] $node + */ + private function findVariableAssignName($node, string $variableName): ?Assign + { + return $this->betterNodeFinder->findFirst((array) $node, function (Node $node) use ($variableName) { + if (! $node instanceof Assign) { + return false; + } + + if (! $node->var instanceof Variable) { + return false; + } + + return $this->isName($node->var, $variableName); + }); + } + + private function getParentNode(Node $node): ?Node + { + $parentNode = $node->getAttribute(AttributeKey::PARENT_NODE); + if ($parentNode instanceof Expression) { + $parentNode = $parentNode->getAttribute(AttributeKey::PARENT_NODE); + } + + return $parentNode; + } + + private function refactorFor(Variable $variable, For_ $for): void + { + $variableName = $this->getName($variable); + + // nothing we can do + if ($variableName === null) { + return; + } + + $assignVariable = $this->findVariableAssignName($for->init, $variableName); + if ($assignVariable === null) { + return; + } + + if ($assignVariable->expr instanceof LNumber) { + $number = $this->getValue($assignVariable->expr); + $assignVariable->expr = new LNumber($number + 1); + } + } +} diff --git a/rules/php-office/tests/Rector/MethodCall/IncreaseColumnIndexRector/Fixture/concat_number.php.inc b/rules/php-office/tests/Rector/MethodCall/IncreaseColumnIndexRector/Fixture/concat_number.php.inc new file mode 100644 index 000000000000..5c1b7f8066af --- /dev/null +++ b/rules/php-office/tests/Rector/MethodCall/IncreaseColumnIndexRector/Fixture/concat_number.php.inc @@ -0,0 +1,33 @@ +setCellValueByColumnAndRow($number + 3, 3, '1150'); + } +} + +?> +----- +setCellValueByColumnAndRow($number + 4, 3, '1150'); + } +} + +?> diff --git a/rules/php-office/tests/Rector/MethodCall/IncreaseColumnIndexRector/Fixture/fixture.php.inc b/rules/php-office/tests/Rector/MethodCall/IncreaseColumnIndexRector/Fixture/fixture.php.inc new file mode 100644 index 000000000000..ffbde9af4f00 --- /dev/null +++ b/rules/php-office/tests/Rector/MethodCall/IncreaseColumnIndexRector/Fixture/fixture.php.inc @@ -0,0 +1,29 @@ +setCellValueByColumnAndRow(0, 3, '1150'); + } +} + +?> +----- +setCellValueByColumnAndRow(1, 3, '1150'); + } +} + +?> diff --git a/rules/php-office/tests/Rector/MethodCall/IncreaseColumnIndexRector/Fixture/for_.php.inc b/rules/php-office/tests/Rector/MethodCall/IncreaseColumnIndexRector/Fixture/for_.php.inc new file mode 100644 index 000000000000..9780d97d3762 --- /dev/null +++ b/rules/php-office/tests/Rector/MethodCall/IncreaseColumnIndexRector/Fixture/for_.php.inc @@ -0,0 +1,39 @@ +setCellValueByColumnAndRow($column, $row, 'value ' . $column); + } + } +} + +?> +----- +setCellValueByColumnAndRow($column, $row, 'value ' . $column); + } + } +} + +?> diff --git a/rules/php-office/tests/Rector/MethodCall/IncreaseColumnIndexRector/Fixture/variable_.php.inc b/rules/php-office/tests/Rector/MethodCall/IncreaseColumnIndexRector/Fixture/variable_.php.inc new file mode 100644 index 000000000000..957345ba1832 --- /dev/null +++ b/rules/php-office/tests/Rector/MethodCall/IncreaseColumnIndexRector/Fixture/variable_.php.inc @@ -0,0 +1,33 @@ +setCellValueByColumnAndRow($number, 3, '1150'); + } +} + +?> +----- +setCellValueByColumnAndRow($number + 1, 3, '1150'); + } +} + +?> diff --git a/rules/php-office/tests/Rector/MethodCall/IncreaseColumnIndexRector/IncreaseColumnIndexRectorTest.php b/rules/php-office/tests/Rector/MethodCall/IncreaseColumnIndexRector/IncreaseColumnIndexRectorTest.php new file mode 100644 index 000000000000..6a62efb2f7ae --- /dev/null +++ b/rules/php-office/tests/Rector/MethodCall/IncreaseColumnIndexRector/IncreaseColumnIndexRectorTest.php @@ -0,0 +1,30 @@ +doTestFile($file); + } + + public function provideData(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + protected function getRectorClass(): string + { + return IncreaseColumnIndexRector::class; + } +}