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 @@ -11,12 +11,10 @@
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
Expand Down Expand Up @@ -71,7 +69,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->isObjectType($node->var, 'PHPExcel_Worksheet')) {
if (! $this->isObjectTypes($node->var, ['PHPExcel_Worksheet', 'PHPExcel_Worksheet_PageSetup'])) {
return null;
}

Expand All @@ -80,25 +78,24 @@ public function refactor(Node $node): ?Node
}

// increase column value
$firstArumentValue = $node->args[0]->value;
if ($firstArumentValue instanceof LNumber) {
++$firstArumentValue->value;
$firstArgumentValue = $node->args[0]->value;
if ($firstArgumentValue instanceof LNumber) {
++$firstArgumentValue->value;
}

if ($firstArumentValue instanceof BinaryOp) {
$this->refactorBinaryOp($firstArumentValue);
if ($firstArgumentValue instanceof BinaryOp) {
$this->refactorBinaryOp($firstArgumentValue);
}

if ($firstArumentValue instanceof Variable) {
$parentNode = $this->getParentNode($node);

if (! $parentNode instanceof For_) {
$node->args[0]->value = new Plus($firstArumentValue, new LNumber(1));
if ($firstArgumentValue instanceof Variable) {
// check if for() value, rather update that
$forDefinedVariableAssignedValue = $this->findPreviousForWithVariable($firstArgumentValue);
if ($forDefinedVariableAssignedValue === null) {
$node->args[0]->value = new Plus($firstArgumentValue, new LNumber(1));
return null;
}

// check if for() value, rather update that
$this->refactorFor($firstArumentValue, $parentNode);
++$forDefinedVariableAssignedValue->value;
}

return $node;
Expand Down Expand Up @@ -135,33 +132,28 @@ private function findVariableAssignName($node, string $variableName): ?Assign
});
}

private function getParentNode(Node $node): ?Node
private function findPreviousForWithVariable(Variable $variable): ?LNumber
{
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof Expression) {
$parentNode = $parentNode->getAttribute(AttributeKey::PARENT_NODE);
/** @var For_|null $for */
$for = $this->betterNodeFinder->findFirstPreviousOfTypes($variable, [For_::class]);
if ($for === null) {
return null;
}

return $parentNode;
}

private function refactorFor(Variable $variable, For_ $for): void
{
$variableName = $this->getName($variable);

// nothing we can do
if ($variableName === null) {
return;
return null;
}

$assignVariable = $this->findVariableAssignName($for->init, $variableName);
if ($assignVariable === null) {
return;
return null;
}

if ($assignVariable->expr instanceof LNumber) {
$number = $this->getValue($assignVariable->expr);
$assignVariable->expr = new LNumber($number + 1);
return $assignVariable->expr;
}

return null;
}
}
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 ForWithCalls
{
public function run(): void
{
$max = 1000;
$row = 5;
$worksheet = new \PHPExcel_Worksheet();

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

?>
-----
<?php

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

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

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

?>