| Conditions | 12 |
| Paths | 1 |
| Total Lines | 29 |
| Code Lines | 22 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 15 | public function boot(\TYPO3\Flow\Core\Bootstrap $bootstrap) { |
||
| 16 | $dispatcher = $bootstrap->getSignalSlotDispatcher(); |
||
| 17 | $newUriPathSegment = NULL; |
||
| 18 | $dispatcher->connect('TYPO3\TYPO3CR\Domain\Model\Node', 'nodePropertyChanged', function(Node $node, $propertyName, $oldValue, $newValue) use($bootstrap, &$newUriPathSegment) { |
||
| 19 | if (method_exists('TYPO3\Neos\Utility\NodeUriPathSegmentGenerator', 'generateUriPathSegment') || method_exists('TYPO3\Neos\Utility\NodeUriPathSegmentGenerator', 'setUniqueUriPathSegment')) { |
||
| 20 | $nodeUriPathSegmentGenerator = $bootstrap->getObjectManager()->get('TYPO3\Neos\Utility\NodeUriPathSegmentGenerator'); |
||
| 21 | } |
||
| 22 | if ($propertyName === 'title' && $node->getNodeType()->isOfType('TYPO3.Neos:Document')) { |
||
| 23 | if (method_exists('TYPO3\Neos\Utility\NodeUriPathSegmentGenerator', 'generateUriPathSegment')) { |
||
| 24 | $newUriPathSegment = strtolower($nodeUriPathSegmentGenerator->generateUriPathSegment($node)); |
||
|
|
|||
| 25 | } else { |
||
| 26 | $newUriPathSegment = strtolower(\TYPO3\TYPO3CR\Utility::renderValidNodeName($node->getProperty('title') ?: $node->getName())); |
||
| 27 | } |
||
| 28 | if (method_exists('TYPO3\Neos\Utility\NodeUriPathSegmentGenerator', 'setUniqueUriPathSegment')) { |
||
| 29 | $nodeUriPathSegmentGenerator->setUniqueUriPathSegment($node); |
||
| 30 | } else { |
||
| 31 | $node->setProperty('uriPathSegment', $newUriPathSegment); |
||
| 32 | } |
||
| 33 | $bootstrap->getObjectManager()->get('TYPO3\Neos\Routing\Cache\RouteCacheFlusher')->registerNodeChange($node); |
||
| 34 | } elseif ($propertyName === 'uriPathSegment' && $newUriPathSegment !== NULL && $newValue !== $newUriPathSegment) { |
||
| 35 | if (method_exists('TYPO3\Neos\Utility\NodeUriPathSegmentGenerator', 'setUniqueUriPathSegment')) { |
||
| 36 | $nodeUriPathSegmentGenerator->setUniqueUriPathSegment($node); |
||
| 37 | } else { |
||
| 38 | $node->setProperty('uriPathSegment', $newUriPathSegment); |
||
| 39 | } |
||
| 40 | $newUriPathSegment = NULL; |
||
| 41 | } |
||
| 42 | }); |
||
| 43 | } |
||
| 44 | |||
| 46 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: