Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rector 0.10 minimal changes #149

Merged
merged 9 commits into from
Jun 8, 2021
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ https://www.palantir.net/rector

## Scope and limitations

The development of this tool is prioritized by the percieved impact of the deprecations and updates. There are many deprecations that often involve several components and for each of these there are several ways to address the deprecation.
The development of this tool is prioritized by the perceived impact of the deprecations and updates. There are many deprecations that often involve several components and for each of these there are several ways to address the deprecation.

We've tried to determine impact based on:
- The use of the deprecated functionality in the contributed modules on Drupal.org
Expand Down Expand Up @@ -111,7 +111,7 @@ Fatal error: Declaration of _HumbugBox3630ef99eac4\Symfony\Component\HttpKernel\
```

You may need to check that you are
- Running `composer install` from an environment that support Php 7.2 or greater
- Running `composer install` from an environment that supports Php 7.2 or greater
- Running Drupal Rector from an environment that supports Php 7.2 or greater

Sometimes people install composer dependencies from one machine (host machine) and run Drupal Rector from another (such as a Lando VM).
Expand Down
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;
}

}