Conditions | 13 |
Paths | 10 |
Total Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
93 | protected function appendFirstUriPartIfValidDimension(&$path) |
||
94 | { |
||
95 | $requestPath = ltrim($this->controllerContext->getRequest()->getHttpRequest()->getUri()->getPath(), '/'); |
||
96 | $matches = []; |
||
97 | preg_match(FrontendNodeRoutePartHandler::DIMENSION_REQUEST_PATH_MATCHER, $requestPath, $matches); |
||
98 | if (!isset($matches['firstUriPart']) && !isset($matches['dimensionPresetUriSegments'])) { |
||
99 | return; |
||
100 | } |
||
101 | |||
102 | $dimensionPresets = $this->contentDimensionPresetSource->getAllPresets(); |
||
103 | if (count($dimensionPresets) === 0) { |
||
104 | return; |
||
105 | } |
||
106 | |||
107 | $firstUriPartExploded = explode('_', $matches['firstUriPart'] ?: $matches['dimensionPresetUriSegments']); |
||
108 | if ($this->supportEmptySegmentForDimensions) { |
||
109 | foreach ($firstUriPartExploded as $uriSegment) { |
||
110 | $uriSegmentIsValid = false; |
||
111 | foreach ($dimensionPresets as $dimensionName => $dimensionPreset) { |
||
112 | $preset = $this->contentDimensionPresetSource->findPresetByUriSegment($dimensionName, $uriSegment); |
||
113 | if ($preset !== null) { |
||
114 | $uriSegmentIsValid = true; |
||
115 | break; |
||
116 | } |
||
117 | } |
||
118 | if (!$uriSegmentIsValid) { |
||
119 | return; |
||
120 | } |
||
121 | } |
||
122 | } else { |
||
123 | if (count($firstUriPartExploded) !== count($dimensionPresets)) { |
||
124 | $this->appendDefaultDimensionPresetUriSegments($dimensionPresets, $path); |
||
125 | return; |
||
126 | } |
||
127 | foreach ($dimensionPresets as $dimensionName => $dimensionPreset) { |
||
128 | $uriSegment = array_shift($firstUriPartExploded); |
||
129 | $preset = $this->contentDimensionPresetSource->findPresetByUriSegment($dimensionName, $uriSegment); |
||
130 | if ($preset === null) { |
||
131 | $this->appendDefaultDimensionPresetUriSegments($dimensionPresets, $path); |
||
132 | return; |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | |||
137 | $path = $matches['firstUriPart'] . '/' . $path; |
||
138 | } |
||
139 | |||
153 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: