Completed
Pull Request — master (#2)
by
unknown
14:34
created

Package::boot()   C

Complexity

Conditions 12
Paths 1

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 29
rs 5.1612
cc 12
eloc 22
nc 1
nop 1

How to fix   Complexity   

Long Method

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:

1
<?php
2
namespace MOC\SynchronizeUrl;
3
4
use TYPO3\Flow\Package\Package as BasePackage;
5
use TYPO3\TYPO3CR\Domain\Model\Node;
6
7
class Package extends BasePackage {
8
9
	/**
10
	 * Invokes custom PHP code directly after the package manager has been initialized.
11
	 *
12
	 * @param \TYPO3\Flow\Core\Bootstrap $bootstrap The current bootstrap
13
	 * @return void
14
	 */
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));
0 ignored issues
show
Bug introduced by
The variable $nodeUriPathSegmentGenerator does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
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
45
}
46