| Conditions | 12 |
| Paths | 4 |
| Total Lines | 77 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 79 | private function generateToC(Crawler $xml): array |
||
| 80 | { |
||
| 81 | $blocksMap = []; |
||
| 82 | |||
| 83 | /** @var array|XApiCmi5Item[] $items */ |
||
| 84 | $items = $xml |
||
| 85 | ->filterXPath('//*') |
||
| 86 | ->reduce( |
||
| 87 | function (Crawler $node, $i) { |
||
| 88 | return \in_array($node->nodeName(), ['au', 'block']); |
||
| 89 | } |
||
| 90 | ) |
||
| 91 | ->each( |
||
| 92 | function (Crawler $node, $i) use (&$blocksMap) { |
||
| 93 | $attributes = ['id', 'activityType', 'launchMethod', 'moveOn', 'masteryScore']; |
||
| 94 | |||
| 95 | list($id, $activityType, $launchMethod, $moveOn, $masteryMode) = $node->extract($attributes)[0]; |
||
| 96 | |||
| 97 | $item = new XApiCmi5Item(); |
||
| 98 | $item |
||
| 99 | ->setIdentifier($id) |
||
| 100 | ->setType($node->nodeName()) |
||
| 101 | ->setTitle( |
||
| 102 | $this->getLanguageStrings( |
||
| 103 | $node->filterXPath('//title') |
||
| 104 | ) |
||
| 105 | ) |
||
| 106 | ->setDescription( |
||
| 107 | $this->getLanguageStrings( |
||
| 108 | $node->filterXPath('//description') |
||
| 109 | ) |
||
| 110 | ) |
||
| 111 | ; |
||
| 112 | |||
| 113 | if ('au' === $node->nodeName()) { |
||
| 114 | $launchParametersNode = $node->filterXPath('//launchParameters'); |
||
| 115 | $entitlementKeyNode = $node->filterXPath('//entitlementKey'); |
||
| 116 | $url |
||
| 117 | = $item |
||
| 118 | ->setUrl( |
||
| 119 | $this->parseLaunchUrl( |
||
| 120 | trim($node->filterXPath('//url')->text()) |
||
| 121 | ) |
||
| 122 | ) |
||
| 123 | ->setActivityType($activityType ?: null) |
||
| 124 | ->setLaunchMethod($launchMethod ?: null) |
||
| 125 | ->setMoveOn($moveOn ?: 'NotApplicable') |
||
| 126 | ->setMasteryScore((float) $masteryMode ?: null) |
||
| 127 | ->setLaunchParameters( |
||
| 128 | $launchParametersNode->count() > 0 ? trim($launchParametersNode->text()) : null |
||
| 129 | ) |
||
| 130 | ->setEntitlementKey( |
||
| 131 | $entitlementKeyNode->count() > 0 ? trim($entitlementKeyNode->text()) : null |
||
| 132 | ) |
||
| 133 | ; |
||
| 134 | } |
||
| 135 | |||
| 136 | $parentNode = $node->parents()->first(); |
||
| 137 | |||
| 138 | if ('block' === $parentNode->nodeName()) { |
||
| 139 | $blocksMap[$i] = $parentNode->attr('id'); |
||
| 140 | } |
||
| 141 | |||
| 142 | return $item; |
||
| 143 | } |
||
| 144 | ) |
||
| 145 | ; |
||
| 146 | |||
| 147 | foreach ($blocksMap as $itemPos => $parentIdentifier) { |
||
| 148 | foreach ($items as $item) { |
||
| 149 | if ($parentIdentifier === $item->getIdentifier()) { |
||
| 150 | $items[$itemPos]->setParent($item); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | return $items; |
||
| 156 | } |
||
| 180 |