| Conditions | 4 |
| Paths | 5 |
| Total Lines | 35 |
| Code Lines | 17 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 18 |
| CRAP Score | 4.0023 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 22 | 10 | public function getOrCreateByNames(array $names, Site $site): array |
|
| 23 | { |
||
| 24 | 10 | if (!$names) { |
|
|
|
|||
| 25 | return []; |
||
| 26 | } |
||
| 27 | |||
| 28 | // Dedup and trim whitespaces |
||
| 29 | 10 | $names = array_unique(array_map(fn ($value) => trim($value), $names)); |
|
| 30 | |||
| 31 | 10 | $artists = $this->findBy([ |
|
| 32 | 10 | 'name' => $names, |
|
| 33 | 10 | 'site' => $site->value, |
|
| 34 | 10 | ]); |
|
| 35 | |||
| 36 | 10 | $found = []; |
|
| 37 | 10 | foreach ($artists as $artist) { |
|
| 38 | // Names are not trimmed when saved from thesaurus interface, so we |
||
| 39 | // have names with trailing whitespace in the database. |
||
| 40 | // With the collation we use, MariaDB trims whitespaces when doing |
||
| 41 | // comparison (affecting UNIQUE constraint and SELECT queries). |
||
| 42 | // So we must be sure to handle whitespaces in the same way as |
||
| 43 | // MariaDB when doing comparisons. |
||
| 44 | 8 | $found[] = trim($artist->getName()); |
|
| 45 | } |
||
| 46 | |||
| 47 | 10 | $notFound = array_diff($names, $found); |
|
| 48 | 10 | foreach ($notFound as $name) { |
|
| 49 | 10 | $artist = new Artist(); |
|
| 50 | 10 | $artist->setSite($site); |
|
| 51 | 10 | $this->getEntityManager()->persist($artist); |
|
| 52 | 10 | $artist->setName($name); |
|
| 53 | 10 | $artists[] = $artist; |
|
| 54 | } |
||
| 55 | |||
| 56 | 10 | return $artists; |
|
| 57 | } |
||
| 59 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.