Conditions | 13 |
Paths | 2 |
Total Lines | 82 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 17 | ||
Bugs | 3 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
96 | public function process($text, $langcode) { |
||
97 | $result = new FilterProcessResult($text); |
||
98 | |||
99 | if (strpos($text, 'data-entity-type') !== FALSE && (strpos($text, 'data-entity-embed-display') !== FALSE || strpos($text, 'data-view-mode') !== FALSE)) { |
||
100 | $dom = Html::load($text); |
||
101 | $xpath = new \DOMXPath($dom); |
||
102 | |||
103 | foreach ($xpath->query('//drupal-entity[@data-entity-type and (@data-entity-uuid or @data-entity-id) and (@data-entity-embed-display or @data-view-mode)]') as $node) { |
||
104 | /** @var \DOMElement $node */ |
||
105 | $entity_type = $node->getAttribute('data-entity-type'); |
||
106 | $entity = NULL; |
||
|
|||
107 | $entity_output = ''; |
||
108 | |||
109 | // data-entity-embed-settings is deprecated, make sure we convert it to |
||
110 | // data-entity-embed-display-settings. |
||
111 | if (($settings = $node->getAttribute('data-entity-embed-settings')) && !$node->hasAttribute('data-entity-embed-display-settings')) { |
||
112 | $node->setAttribute('data-entity-embed-display-settings', $settings); |
||
113 | $node->removeAttribute('data-entity-embed-settings'); |
||
114 | } |
||
115 | |||
116 | try { |
||
117 | // Load the entity either by UUID (preferred) or ID. |
||
118 | $id = NULL; |
||
119 | $entity = NULL; |
||
120 | if ($id = $node->getAttribute('data-entity-uuid')) { |
||
121 | $entity = $this->entityTypeManager->getStorage($entity_type) |
||
122 | ->loadByProperties(['uuid' => $id]); |
||
123 | $entity = current($entity); |
||
124 | } |
||
125 | else { |
||
126 | $id = $node->getAttribute('data-entity-id'); |
||
127 | $entity = $this->entityTypeManager->getStorage($entity_type)->load($id); |
||
128 | } |
||
129 | |||
130 | if ($entity) { |
||
131 | // Protect ourselves from recursive rendering. |
||
132 | static $depth = 0; |
||
133 | $depth++; |
||
134 | if ($depth > 20) { |
||
135 | throw new RecursiveRenderingException(sprintf('Recursive rendering detected when rendering embedded %s entity %s.', $entity_type, $entity->id())); |
||
136 | } |
||
137 | |||
138 | // If a UUID was not used, but is available, add it to the HTML. |
||
139 | if (!$node->getAttribute('data-entity-uuid') && $uuid = $entity->uuid()) { |
||
140 | $node->setAttribute('data-entity-uuid', $uuid); |
||
141 | } |
||
142 | |||
143 | $context = $this->getNodeAttributesAsArray($node); |
||
144 | $context += array('data-langcode' => $langcode); |
||
145 | $build = $this->builder->buildEntityEmbed($entity, $context); |
||
146 | // We need to render the embedded entity: |
||
147 | // - without replacing placeholders, so that the placeholders are |
||
148 | // only replaced at the last possible moment. Hence we cannot use |
||
149 | // either renderPlain() or renderRoot(), so we must use render(). |
||
150 | // - without bubbling beyond this filter, because filters must |
||
151 | // ensure that the bubbleable metadata for the changes they make |
||
152 | // when filtering text makes it onto the FilterProcessResult |
||
153 | // object that they return ($result). To prevent that bubbling, we |
||
154 | // must wrap the call to render() in a render context. |
||
155 | $entity_output = $this->renderer->executeInRenderContext(new RenderContext(), function () use (&$build) { |
||
156 | return $this->renderer->render($build); |
||
157 | }); |
||
158 | $result = $result->merge(BubbleableMetadata::createFromRenderArray($build)); |
||
159 | |||
160 | $depth--; |
||
161 | } |
||
162 | else { |
||
163 | throw new EntityNotFoundException(sprintf('Unable to load embedded %s entity %s.', $entity_type, $id)); |
||
164 | } |
||
165 | } |
||
166 | catch(\Exception $e) { |
||
167 | watchdog_exception('entity_embed', $e); |
||
168 | } |
||
169 | |||
170 | $this->replaceNodeContent($node, $entity_output); |
||
171 | } |
||
172 | |||
173 | $result->setProcessedText(Html::serialize($dom)); |
||
174 | } |
||
175 | |||
176 | return $result; |
||
177 | } |
||
178 | |||
194 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.