| Conditions | 15 |
| Paths | 243 |
| Total Lines | 86 |
| Code Lines | 50 |
| 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 |
||
| 92 | public function createSlug(EntityInterface $entity, string $target): string |
||
| 93 | {
|
||
| 94 | $config = $this->getConfig($target); |
||
| 95 | |||
| 96 | if ($entity->isDirty($config['source'])) {
|
||
| 97 | if ((mb_strlen($config['replacement']) + 1) < $config['length']) {
|
||
| 98 | if (isset($config['method'])) {
|
||
| 99 | if (method_exists($this->getTable(), $config['method'])) {
|
||
| 100 | $slug = $this->getTable()->{$config['method']}($entity->{$config['source']}, $config);
|
||
| 101 | } else {
|
||
| 102 | throw new MethodException(__d('slug', 'Method {0} does not exist.', $config['method']));
|
||
| 103 | } |
||
| 104 | } else {
|
||
| 105 | $slug = Text::slug(mb_strtolower($entity->{$config['source']}), [
|
||
| 106 | 'replacement' => $config['replacement'], |
||
| 107 | ]); |
||
| 108 | } |
||
| 109 | |||
| 110 | $slugs = $this->sortSlugs($this->getSlugs($slug, $target)); |
||
| 111 | |||
| 112 | // Slug is just numbers |
||
| 113 | if (preg_match('/^[0-9]+$/', $slug)) {
|
||
| 114 | $numbers = preg_grep('/^[0-9]+$/', $slugs);
|
||
| 115 | |||
| 116 | if (!empty($numbers)) {
|
||
| 117 | sort($numbers); |
||
| 118 | |||
| 119 | $slug = end($numbers); |
||
| 120 | |||
| 121 | $slug++; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | // Cut slug |
||
| 126 | if (mb_strlen($replace = preg_replace('/\s+/', $config['replacement'], $slug)) > $config['length']) {
|
||
| 127 | $slug = mb_substr($replace, 0, $config['length']); |
||
| 128 | |||
| 129 | // Update slug list based on cut slug |
||
| 130 | $slugs = $this->sortSlugs($this->getSlugs($slug, $field)); |
||
| 131 | } |
||
| 132 | |||
| 133 | $slug = preg_replace('/' . preg_quote($config['replacement']) . '$/', '', trim(mb_substr($slug, 0, $config['length'])));
|
||
| 134 | |||
| 135 | if (in_array($slug, $slugs)) {
|
||
| 136 | $list = preg_grep('/^' . preg_replace('/' . preg_quote($config['replacement']) . '([1-9]{1}[0-9]*)$/', $config['replacement'], $slug) . '/', $slugs);
|
||
| 137 | |||
| 138 | preg_match('/^(.*)' . preg_quote($config['replacement']) . '([1-9]{1}[0-9]*)$/', end($list), $matches);
|
||
| 139 | |||
| 140 | if (empty($matches)) {
|
||
| 141 | $increment = 1; |
||
| 142 | } else {
|
||
| 143 | if (isset($matches[2])) {
|
||
| 144 | $increment = $matches[2] += 1; |
||
| 145 | } else {
|
||
| 146 | throw new IncrementException(__d('slug', 'Cannot create next suffix because matches are empty.'));
|
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | if (mb_strlen($slug . $config['replacement'] . $increment) <= $config['length']) {
|
||
| 151 | $string = $slug; |
||
| 152 | } elseif (mb_strlen(mb_substr($slug, 0, -mb_strlen($increment))) + mb_strlen($config['replacement'] . $increment) <= $config['length']) {
|
||
| 153 | $string = mb_substr($slug, 0, $config['length'] - mb_strlen($config['replacement'] . $increment)); |
||
| 154 | } else {
|
||
| 155 | $string = mb_substr($slug, 0, -(mb_strlen($config['replacement'] . $increment))); |
||
| 156 | } |
||
| 157 | |||
| 158 | if (mb_strlen($string) > 0) {
|
||
| 159 | $slug = $string . $config['replacement'] . $increment; |
||
| 160 | |||
| 161 | // Refresh slugs list |
||
| 162 | $slugs = $this->sortSlugs(array_merge($slugs, $this->getSlugs($slug, $target))); |
||
| 163 | |||
| 164 | if (in_array($slug, $slugs)) {
|
||
| 165 | return $this->createSlug($slug, $target); |
||
| 166 | } |
||
| 167 | } else {
|
||
| 168 | throw new LengthException(__d('slug', 'Cannot create slug because there are no available names.'));
|
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | return $slug; |
||
| 173 | } else {
|
||
| 174 | throw new LimitException(__d('slug', 'Limit of length in {0} field is too short.', $target));
|
||
| 175 | } |
||
| 176 | } else {
|
||
| 177 | return $entity->{$target};
|
||
| 178 | } |
||
| 224 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths