| Conditions | 16 |
| 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 |
||
| 101 | public function createSlug($entity, string $target): string |
||
| 102 | {
|
||
| 103 | $config = $this->getConfig($target); |
||
| 104 | |||
| 105 | if ($entity->isDirty($config['source']) || empty($entity->{$target})) {
|
||
| 106 | if ((mb_strlen($config['replacement']) + 1) < $config['length']) {
|
||
| 107 | if (isset($config['method'])) {
|
||
| 108 | if (method_exists($this->table(), $config['method'])) {
|
||
| 109 | $slug = $this->table()->{$config['method']}($entity, $config);
|
||
| 110 | } else {
|
||
| 111 | throw new MethodException(__d('slug', 'Method {0} does not exist.', $config['method']));
|
||
| 112 | } |
||
| 113 | } else {
|
||
| 114 | $slug = Text::slug(mb_strtolower($entity->{$config['source']}), [
|
||
| 115 | 'replacement' => $config['replacement'], |
||
| 116 | ]); |
||
| 117 | } |
||
| 118 | |||
| 119 | $slugs = $this->sortSlugs($this->getSlugs($slug, $target)); |
||
| 120 | |||
| 121 | // Slug is just numbers |
||
| 122 | if (preg_match('/^[0-9]+$/', $slug)) {
|
||
| 123 | $numbers = preg_grep('/^[0-9]+$/', $slugs);
|
||
| 124 | |||
| 125 | if (!empty($numbers)) {
|
||
| 126 | sort($numbers); |
||
| 127 | |||
| 128 | $slug = end($numbers); |
||
| 129 | |||
| 130 | $slug++; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | // Cut slug |
||
| 135 | if (mb_strlen($replace = preg_replace('/\s+/', $config['replacement'], $slug)) > $config['length']) {
|
||
| 136 | $slug = mb_substr($replace, 0, $config['length']); |
||
| 137 | |||
| 138 | // Update slug list based on cut slug |
||
| 139 | $slugs = $this->sortSlugs($this->getSlugs($slug, $target)); |
||
| 140 | } |
||
| 141 | |||
| 142 | $slug = preg_replace('/' . preg_quote($config['replacement']) . '$/', '', trim(mb_substr($slug, 0, $config['length'])));
|
||
| 143 | |||
| 144 | if (in_array($slug, $slugs)) {
|
||
| 145 | $list = preg_grep('/^' . preg_replace('/' . preg_quote($config['replacement']) . '([1-9]{1}[0-9]*)$/', $config['replacement'], $slug) . '/', $slugs);
|
||
| 146 | |||
| 147 | preg_match('/^(.*)' . preg_quote($config['replacement']) . '([1-9]{1}[0-9]*)$/', end($list), $matches);
|
||
| 148 | |||
| 149 | if (empty($matches)) {
|
||
| 150 | $increment = 1; |
||
| 151 | } else {
|
||
| 152 | if (isset($matches[2])) {
|
||
| 153 | $increment = $matches[2] += 1; |
||
| 154 | } else {
|
||
| 155 | throw new IncrementException(__d('slug', 'Cannot create next suffix because matches are empty.'));
|
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | if (mb_strlen($slug . $config['replacement'] . $increment) <= $config['length']) {
|
||
| 160 | $string = $slug; |
||
| 161 | } elseif (mb_strlen(mb_substr($slug, 0, -mb_strlen($increment))) + mb_strlen($config['replacement'] . $increment) <= $config['length']) {
|
||
| 162 | $string = mb_substr($slug, 0, $config['length'] - mb_strlen($config['replacement'] . $increment)); |
||
| 163 | } else {
|
||
| 164 | $string = mb_substr($slug, 0, -(mb_strlen($config['replacement'] . $increment))); |
||
| 165 | } |
||
| 166 | |||
| 167 | if (mb_strlen($string) > 0) {
|
||
| 168 | $slug = $string . $config['replacement'] . $increment; |
||
| 169 | |||
| 170 | // Refresh slugs list |
||
| 171 | $slugs = $this->sortSlugs(array_merge($slugs, $this->getSlugs($slug, $target))); |
||
| 172 | |||
| 173 | if (in_array($slug, $slugs)) {
|
||
| 174 | return $this->createSlug($slug, $target); |
||
| 175 | } |
||
| 176 | } else {
|
||
| 177 | throw new LengthException(__d('slug', 'Cannot create slug because there are no available names.'));
|
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | return $slug; |
||
| 182 | } else {
|
||
| 183 | throw new LimitException(__d('slug', 'Limit of length in {0} field is too short.', $target));
|
||
| 184 | } |
||
| 185 | } else {
|
||
| 186 | return $entity->{$target};
|
||
| 187 | } |
||
| 233 |