| Conditions | 4 |
| Paths | 5 |
| Total Lines | 29 |
| Code Lines | 17 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 18 |
| CRAP Score | 4.0023 |
| Changes | 0 | ||
| 1 | <?php |
||
| 21 | 10 | public function getOrCreateByNames(array $names, string $site): array |
|
| 22 | { |
||
| 23 | 10 | if (!$names) { |
|
|
|
|||
| 24 | return []; |
||
| 25 | } |
||
| 26 | |||
| 27 | // Dedup and trim whitespaces |
||
| 28 | 10 | $names = array_unique(array_map(fn ($value) => trim($value), $names)); |
|
| 29 | |||
| 30 | 10 | $artists = $this->findBy([ |
|
| 31 | 10 | 'name' => $names, |
|
| 32 | 10 | 'site' => $site, |
|
| 33 | 10 | ]); |
|
| 34 | |||
| 35 | 10 | $found = []; |
|
| 36 | 10 | foreach ($artists as $artist) { |
|
| 37 | 8 | $found[] = $artist->getName(); |
|
| 38 | } |
||
| 39 | |||
| 40 | 10 | $notFound = array_diff($names, $found); |
|
| 41 | 10 | foreach ($notFound as $name) { |
|
| 42 | 10 | $artist = new Artist(); |
|
| 43 | 10 | $artist->setSite($site); |
|
| 44 | 10 | $this->getEntityManager()->persist($artist); |
|
| 45 | 10 | $artist->setName($name); |
|
| 46 | 10 | $artists[] = $artist; |
|
| 47 | } |
||
| 48 | |||
| 49 | 10 | return $artists; |
|
| 50 | } |
||
| 52 |
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.