| Conditions | 7 |
| Paths | 15 |
| Total Lines | 51 |
| Code Lines | 31 |
| 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 |
||
| 82 | private function hasConflicts() |
||
| 83 | { |
||
| 84 | $routeEntity = $this->entityRef; |
||
| 85 | |||
| 86 | $newPath = $routeEntity->getPathWithBundlePrefix(); |
||
| 87 | |||
| 88 | /** @var RouteCollection $routeCollection */ |
||
| 89 | $routeCollection = $this->router->getRouteCollection(); |
||
| 90 | |||
| 91 | $errors = []; |
||
| 92 | foreach ($routeCollection->all() as $route) { |
||
| 93 | $path = $route->getPath(); |
||
| 94 | if ($path === '/{url}') { |
||
| 95 | continue; |
||
| 96 | } |
||
| 97 | |||
| 98 | if ($path === $newPath) { |
||
| 99 | $errors[] = [ |
||
| 100 | 'type' => 'SAME', |
||
| 101 | 'path' => $path |
||
| 102 | ]; |
||
| 103 | continue; |
||
| 104 | } |
||
| 105 | |||
| 106 | $pathRegExp = preg_quote(preg_replace("/{(.+)}/", "____DUMMY____", $path), '/'); |
||
| 107 | $pathRegExp = "#^" . str_replace('____DUMMY____', '(.+)', $pathRegExp) . "$#"; |
||
| 108 | |||
| 109 | $matches = []; |
||
| 110 | preg_match($pathRegExp, $newPath, $matches); |
||
| 111 | if (count($matches)) { |
||
| 112 | $errors[] = [ |
||
| 113 | 'type' => 'SIMILAR', |
||
| 114 | 'path' => $path |
||
| 115 | ]; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | $hasCriticalErrors = false; |
||
| 120 | |||
| 121 | foreach ($errors as $error) { |
||
| 122 | if ($error['type'] == 'SAME') { |
||
| 123 | $message = $this->__('It looks like you created or updated a route with a path which already exists. This is an error in most cases.'); |
||
| 124 | $hasCriticalErrors = true; |
||
| 125 | } else { |
||
| 126 | $message = $this->__f('The path of the route you created or updated looks similar to the following already existing path: %s Are you sure you haven\'t just introduced a conflict?', ['%s' => $error['path']]); |
||
| 127 | } |
||
| 128 | $this->request->getSession()->getFlashBag()->add('error', $message); |
||
| 129 | } |
||
| 130 | |||
| 131 | return $hasCriticalErrors; |
||
| 132 | } |
||
| 133 | } |
||
| 134 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: