Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Plugin / DMU / Converter / Functions / EntityLoad.php
1 <?php
2
3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter\Functions;
4
5 use Drupal\drupalmoduleupgrader\TargetInterface;
6 use Pharborist\Functions\FunctionCallNode;
7 use Pharborist\Objects\ClassMethodCallNode;
8 use Pharborist\Types\ArrayNode;
9
10 /**
11  * @Converter(
12  *  id = "entity_load",
13  *  description = @Translation("Rewrites calls to entity_load().")
14  * )
15  */
16 class EntityLoad extends FunctionCallModifier {
17
18   /**
19    * {@inheritdoc}
20    */
21   public function rewrite(FunctionCallNode $call, TargetInterface $target) {
22     $arguments = $call->getArguments();
23
24     // If there were three arguments, the call is affecting the internal
25     // entity cache. Unfortunately, it's pretty much impossible to reliably
26     // determine whether or not they wanted to reset the cache, so let's just
27     // leave a FIXME.
28     if (sizeof($arguments) == 3) {
29       $this->buildFixMe('To reset the entity cache, use EntityStorageInterface::resetCache().')->insertBefore($call);
30     }
31
32     $rewritten = ClassMethodCallNode::create('\Drupal', 'entityManager')
33       ->appendMethodCall('getStorage')
34       ->appendArgument(clone $arguments[0]);
35
36     // If there's a third argument, conditions were passed. Not a recommended
37     // practice, but certain modules might have done it anyway. If we detect
38     // conditions, use loadByProperties().
39     if (sizeof($arguments) > 2) {
40       return $rewritten
41         ->appendMethodCall('loadByProperties')
42         ->appendArgument(clone $arguments[2]);
43     }
44     else {
45       $rewritten->appendMethodCall('load');
46
47       if (sizeof($arguments) > 1 && $arguments[1] instanceof ArrayNode) {
48         $rewritten->appendArgument(clone $arguments[1]);
49       }
50
51       return $rewritten;
52     }
53   }
54
55 }