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 @@ -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:
Expand Down
23 changes: 22 additions & 1 deletion docs/AllRectorsOverview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# All 496 Rectors Overview
# All 497 Rectors Overview

- [Projects](#projects)
- [General](#general)
Expand Down Expand Up @@ -5328,6 +5328,27 @@ Methods to (new Worksheet())->getDefaultStyle() to getParent()->getDefaultStyle(

<br>

### `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');
}
}
```

<br>

### `RemoveSetTempDirOnExcelWriterRector`

- class: [`Rector\PHPOffice\Rector\MethodCall\RemoveSetTempDirOnExcelWriterRector`](/../master/rules/php-office/src/Rector/MethodCall/RemoveSetTempDirOnExcelWriterRector.php)
Expand Down
2 changes: 1 addition & 1 deletion helpers/extract_default_value_changes_from_diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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#'
167 changes: 167 additions & 0 deletions rules/php-office/src/Rector/MethodCall/IncreaseColumnIndexRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php

declare(strict_types=1);

namespace Rector\PHPOffice\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\Plus;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\For_;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\NodeTypeResolver\Node\AttributeKey;

/**
* @see https://github.com/PHPOffice/PhpSpreadsheet/blob/master/docs/topics/migration-from-PHPExcel.md#column-index-based-on-1
*
* @see \Rector\PHPOffice\Tests\Rector\MethodCall\IncreaseColumnIndexRector\IncreaseColumnIndexRectorTest
*/
final class IncreaseColumnIndexRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition(
'Column index changed from 0 to 1 - run only ONCE! changes current value without memory',
[
new CodeSample(
<<<'PHP'
final class SomeClass
{
public function run(): void
{
$worksheet = new \PHPExcel_Worksheet();
$worksheet->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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\PHPOffice\Tests\Rector\MethodCall\IncreaseColumnIndexRector\Fixture;

final class PlusNumber
{
public function run(): void
{
$worksheet = new \PHPExcel_Worksheet();

$number = 55;
$worksheet->setCellValueByColumnAndRow($number + 3, 3, '1150');
}
}

?>
-----
<?php

namespace Rector\PHPOffice\Tests\Rector\MethodCall\IncreaseColumnIndexRector\Fixture;

final class PlusNumber
{
public function run(): void
{
$worksheet = new \PHPExcel_Worksheet();

$number = 55;
$worksheet->setCellValueByColumnAndRow($number + 4, 3, '1150');
}
}

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

namespace Rector\PHPOffice\Tests\Rector\MethodCall\IncreaseColumnIndexRector\Fixture;

final class SomeClass
{
public function run(): void
{
$worksheet = new \PHPExcel_Worksheet();
$worksheet->setCellValueByColumnAndRow(0, 3, '1150');
}
}

?>
-----
<?php

namespace Rector\PHPOffice\Tests\Rector\MethodCall\IncreaseColumnIndexRector\Fixture;

final class SomeClass
{
public function run(): void
{
$worksheet = new \PHPExcel_Worksheet();
$worksheet->setCellValueByColumnAndRow(1, 3, '1150');
}
}

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

namespace Rector\PHPOffice\Tests\Rector\MethodCall\IncreaseColumnIndexRector\Fixture;

final class For_
{
public function run(): void
{
$max = 1000;
$row = 5;
$worksheet = new \PHPExcel_Worksheet();

for ($column = 0; $column < $max; $column++) {
$worksheet->setCellValueByColumnAndRow($column, $row, 'value ' . $column);
}
}
}

?>
-----
<?php

namespace Rector\PHPOffice\Tests\Rector\MethodCall\IncreaseColumnIndexRector\Fixture;

final class For_
{
public function run(): void
{
$max = 1000;
$row = 5;
$worksheet = new \PHPExcel_Worksheet();

for ($column = 1; $column < $max; $column++) {
$worksheet->setCellValueByColumnAndRow($column, $row, 'value ' . $column);
}
}
}

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

namespace Rector\PHPOffice\Tests\Rector\MethodCall\IncreaseColumnIndexRector\Fixture;

final class Variable
{
public function run(): void
{
$worksheet = new \PHPExcel_Worksheet();

$number = 55;
$worksheet->setCellValueByColumnAndRow($number, 3, '1150');
}
}

?>
-----
<?php

namespace Rector\PHPOffice\Tests\Rector\MethodCall\IncreaseColumnIndexRector\Fixture;

final class Variable
{
public function run(): void
{
$worksheet = new \PHPExcel_Worksheet();

$number = 55;
$worksheet->setCellValueByColumnAndRow($number + 1, 3, '1150');
}
}

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

declare(strict_types=1);

namespace Rector\PHPOffice\Tests\Rector\MethodCall\IncreaseColumnIndexRector;

use Iterator;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\PHPOffice\Rector\MethodCall\IncreaseColumnIndexRector;

final class IncreaseColumnIndexRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $file): void
{
$this->doTestFile($file);
}

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

protected function getRectorClass(): string
{
return IncreaseColumnIndexRector::class;
}
}