Skip to content

Commit

Permalink
Merge pull request #149 from palantirnet/rector-0.10-minimal-changes
Browse files Browse the repository at this point in the history
Rector 0.10 minimal changes
  • Loading branch information
agentrickard authored Jun 8, 2021
2 parents a2a0a38 + 4e38c32 commit 480a920
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/drupal-8/drupal-8.0-deprecations.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use DrupalRector\Rector\Deprecation\EntityInterfaceUrlInfoRector;
use DrupalRector\Rector\Deprecation\EntityLoadRector;
use DrupalRector\Rector\Deprecation\EntityManagerRector;
use DrupalRector\Rector\Deprecation\EntityViewRector;
use DrupalRector\Rector\Deprecation\FileLoadRector;
use DrupalRector\Rector\Deprecation\FormatDateRector;
use DrupalRector\Rector\Deprecation\LinkGeneratorTraitLRector;
Expand Down Expand Up @@ -59,6 +60,8 @@

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

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

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

$services->set(FormatDateRector::class);
Expand Down
5 changes: 5 additions & 0 deletions deprecation-index.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
PHPStan: 'Call to deprecated function entity_load(). Deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use the entity type storage''s load() method.'
Examples:
- entity_load.php
'entity_view()':
Rector: EntityViewRector.php
PHPStan: 'Use the entity view builder''s view() method for creating a render array'
Examples:
- entity_view.php
'node_load()':
Rector: NodeLoadRector.php
PHPStan: 'Call to deprecated function node_load(). Deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use Drupal\node\Entity\Node::load().'
Expand Down
36 changes: 36 additions & 0 deletions rector_examples/entity_view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* This demonstrates the deprecated static calls that might be called from procedural code like `.module` files.
*/

/**
* A simple example.
*/
function simple_example() {
$entity = new stdClass();
$view = entity_view($entity, 'default');
}

/**
* An example using all of the arguments.
*/
function lagncode_example() {
$entity = new stdClass();
$langcode = 'de';
$view = entity_view($entity, 'default', $langcode);
}

function reset_cache_example() {
// Not supported.
}

/**
* An example using arguments as member values.
*/
function arguments_member_values() {
$entity = new stdClass();
$entity->field_view_mode = 'test';

$view = entity_view($entity, $entity->field_view_mode);
}
36 changes: 36 additions & 0 deletions rector_examples_updated/entity_view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* This demonstrates the deprecated static calls that might be called from procedural code like `.module` files.
*/

/**
* A simple example.
*/
function simple_example() {
$entity = new stdClass();
$view = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->view($entity, 'default');
}

/**
* An example using all of the arguments.
*/
function lagncode_example() {
$entity = new stdClass();
$langcode = 'de';
$view = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->view($entity, 'default', $langcode);
}

function reset_cache_example() {
// Not supported.
}

/**
* An example using arguments as member values.
*/
function arguments_member_values() {
$entity = new stdClass();
$entity->field_view_mode = 'test';

$view = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->view($entity, $entity->field_view_mode);
}
95 changes: 95 additions & 0 deletions src/Rector/Deprecation/EntityViewRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace DrupalRector\Rector\Deprecation;

use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Rector\Core\Rector\AbstractRector;
use PhpParser\Node;


/**
* Replaced deprecated entity_view() calls.
*
* See https://www.drupal.org/node/3033656 for change record.
*
* What is covered:
* - Static replacement
* - The reset parameter is excluded.
*
* Improvement opportunities
* - Include support for cache rest parameter.
*/
final class EntityViewRector extends AbstractRector
{

/**
* @inheritdoc
*/
public function getNodeTypes(): array
{
return [
Node\Expr\FuncCall::class,
];
}

/**
* @inheritdoc
*/
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Fixes deprecated entity_view() use',[
new CodeSample(
<<<'CODE_BEFORE'
$rendered = entity_view($entity, 'default');
CODE_BEFORE
,
<<<'CODE_AFTER'
$rendered = \Drupal::entityTypeManager()->getViewBuilder($entity
->getEntityTypeId())->view($entity, 'default');
CODE_AFTER
)
]);
}

/**
* @inheritdoc
*/
public function refactor(Node $node): ?Node
{
if ($this->getName($node->name) !== 'entity_view') {
return NULL;
}

$name = new Node\Name\FullyQualified('Drupal');

$entityTypManager = new Node\Identifier('entityTypeManager');

$var = new Node\Expr\StaticCall($name, $entityTypManager);

$getViewBuilder_method_name = new Node\Identifier('getViewBuilder');

$entity_reference = $node->args[0]->value;
$getEntityTypeId_method_name = new Node\Identifier('getEntityTypeId');

$entityRef_type_id = new Node\Expr\MethodCall($entity_reference, $getEntityTypeId_method_name);

$view_builder = new Node\Expr\MethodCall($var, $getViewBuilder_method_name, [$entityRef_type_id]);

$view_method_name = new Node\Identifier('view');

$view_args = [
$node->args[0]->value,
$node->args[1]->value,
];

if (isset($node->args[2])) {
$view_args[] = $node->args[2]->value;
}

$view = new Node\Expr\MethodCall($view_builder, $view_method_name, $view_args);

return $view;
}

}

0 comments on commit 480a920

Please sign in to comment.