Skip to content

Commit

Permalink
Merge pull request #166 from bluehorndigital/3225019-bump-rector-min
Browse files Browse the repository at this point in the history
Issue #3225019: Could not process due to Class 'ReflectionUnionType' not found
  • Loading branch information
agentrickard authored Jul 29, 2021
2 parents b2f9565 + 1a4fa92 commit 92d56b1
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 4 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"ast"
],
"require": {
"rector/rector": "~0.11.0",
"rector/rector": "~0.11.39",
"webflo/drupal-finder": "^1.2"
},
"license": "MIT",
Expand Down Expand Up @@ -63,7 +63,7 @@
"phpstan/phpstan": "^0.12.82",
"phpstan/phpstan-deprecation-rules": "^0.12.6",
"phpunit/phpunit": "^9.5",
"rector/rector-src": "~0.11.0",
"rector/rector-src": "~0.11.39",
"symfony/yaml": "^5"
}
}
1 change: 1 addition & 0 deletions fixtures/d8/rector_examples_updated/drupal_set_message.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ function message_type_as_variable() {

// TODO: Drupal Rector Notice: Please delete the following comment after you've made any necessary changes.
// This needs to be replaced, but Rector was not yet able to replace this because the type of message was set with a variable. If you need to continue to use a variable, you might consider using a switch statement.
// @noRector
drupal_set_message($message, $type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function message_type_as_variable() {

// TODO: Drupal Rector Notice: Please delete the following comment after you've made any necessary changes.
// This needs to be replaced, but Rector was not yet able to replace this because the type of message was set with a variable. If you need to continue to use a variable, you might consider using a switch statement.
// @noRector
drupal_set_message($message, $type);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function message_type_as_variable() {

// TODO: Drupal Rector Notice: Please delete the following comment after you've made any necessary changes.
// This needs to be replaced, but Rector was not yet able to replace this because the type of message was set with a variable. If you need to continue to use a variable, you might consider using a switch statement.
// @noRector
drupal_set_message($message, $type);
}

Expand Down
25 changes: 24 additions & 1 deletion src/Rector/Deprecation/DrupalSetMessageRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

use DrupalRector\Utility\AddCommentTrait;
use DrupalRector\Utility\TraitsByClassHelperTrait;
use PhpParser\Comment;
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -115,7 +119,26 @@ public function refactor(Node $node): ?Node
* https://git.drupalcode.org/project/devel/blob/8.x-2.0/devel.module#L151
* https://git.drupalcode.org/project/devel/blob/8.x-2.0/devel.module#L265
*/
$this->addDrupalRectorComment($node, 'This needs to be replaced, but Rector was not yet able to replace this because the type of message was set with a variable. If you need to continue to use a variable, you might consider using a switch statement.');
$this->addDrupalRectorComment(
$node,
'This needs to be replaced, but Rector was not yet able to replace this because the type of message was set with a variable. If you need to continue to use a variable, you might consider using a switch statement.'
);
// Since we did not rename the function, Rector will process
// this node multiple times. So we need to flag it with the
// @noRector tag.
$parent_node = $node->getAttribute(AttributeKey::PARENT_NODE);
assert($parent_node instanceof Node);
$comments = $parent_node->getAttribute(AttributeKey::COMMENTS);
$comments[] = new Comment('// @noRector');
$parent_node->setAttribute(AttributeKey::COMMENTS, $comments);

// The comments for this node have already been processed
// and stored in an object hash. We need to manually add the
// tag ourselves to the phpDoc object to prevent further
// processing.
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
assert($phpDocInfo instanceof PhpDocInfo);
$phpDocInfo->addPhpDocTagNode(new PhpDocTagNode('@noRector', new GenericTagValueNode('')));

return $node;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Utility/AddCommentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function addDrupalRectorComment(Node $node, $comment) {
$comments = $statement_node->getComments();
$comments[] = new Comment($comment_with_wrapper);

$statement_node->setAttribute('comments', $comments);
$statement_node->setAttribute(AttributeKey::COMMENTS, $comments);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types=1);

namespace DrupalRector\Tests\Rector\Deprecation\DrupalSetMessageRector;

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

class DrupalSetMessageRectorTest extends AbstractRectorTestCase {

/**
* @covers ::refactor
* @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
{
// must be implemented
return __DIR__ . '/config/configured_rule.php';
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

use DrupalRector\Rector\Deprecation\DrupalSetMessageRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();

$services->set(DrupalSetMessageRector::class);

$parameters = $containerConfigurator->parameters();
$parameters->set('drupal_rector_notices_as_comments', true);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* An example using all of the arguments.
*/
function using_all_arguments() {
drupal_set_message('example warning', 'status', TRUE);
}
?>
-----
<?php

/**
* An example using all of the arguments.
*/
function using_all_arguments() {
\Drupal::messenger()->addStatus('example warning', TRUE);
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* A simple example using the minimum number of arguments.
*/
function simple_example() {
drupal_set_message('example message');
}
?>
-----
<?php

/**
* A simple example using the minimum number of arguments.
*/
function simple_example() {
\Drupal::messenger()->addStatus('example message');
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* Examples that show situations where we define the type of message.
*/
function message_types() {
drupal_set_message('example error', 'error');

drupal_set_message('example status', 'status');

drupal_set_message('example warning', 'warning');
}
?>
-----
<?php

/**
* Examples that show situations where we define the type of message.
*/
function message_types() {
\Drupal::messenger()->addError('example error');

\Drupal::messenger()->addStatus('example status');

\Drupal::messenger()->addWarning('example warning');
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* This shows using a variable as the message type.
*
* This is rare, but used in Devel.
*/
function message_type_as_variable() {
$message = 'example message from variable';

$type = 'warning';

drupal_set_message($message, $type);
}
?>
-----
<?php

/**
* This shows using a variable as the message type.
*
* This is rare, but used in Devel.
*/
function message_type_as_variable() {
$message = 'example message from variable';

$type = 'warning';

// TODO: Drupal Rector Notice: Please delete the following comment after you've made any necessary changes.
// This needs to be replaced, but Rector was not yet able to replace this because the type of message was set with a variable. If you need to continue to use a variable, you might consider using a switch statement.
// @noRector
drupal_set_message($message, $type);
}
?>

0 comments on commit 92d56b1

Please sign in to comment.