Passed
Push — 3.x ( 44cdb4...47c35b )
by Aleksei
13:33
created

EntityUtils::hasParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Annotated\Utils;
6
7
use Cycle\Annotated\Annotation\Entity;
8
use Cycle\Annotated\Entities;
9
use Doctrine\Inflector\Rules\English\InflectorFactory;
10
use Spiral\Attributes\ReaderInterface;
11
12
/**
13
 * @internal
14
 */
15
class EntityUtils
16
{
17
    private \Doctrine\Inflector\Inflector $inflector;
18
19 1056
    public function __construct(private ReaderInterface $reader)
20
    {
21 1056
        $this->inflector = (new InflectorFactory())->build();
22 1056
    }
23
24
    /**
25
     * @param class-string $class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
26
     */
27 1032
    public function hasParent(string $class, bool $root = true): bool
28
    {
29 1032
        return $this->findParent($class, $root) !== null;
30
    }
31
32
    /**
33
     * @param class-string $class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
34
     *
35
     * @return class-string|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string|null.
Loading history...
36
     */
37 1032
    public function findParent(string $class, bool $root = true): ?string
38
    {
39
        /** @var \ReflectionClass[] $parents */
40 1032
        $parents = $this->findParents($class);
41
42 1032
        $parents = $root ? \array_reverse($parents) : $parents;
43
44 1032
        return isset($parents[0]) ? $parents[0]->getName() : null;
45
    }
46 688
47
    public function findParents(string $class): array
48
    {
49
        $parents = [];
50
        /** @var class-string[] $classParents */
51 688
        $classParents = \class_parents($class);
52
53
        foreach ($classParents as $parent) {
54
            try {
55 688
                $class = new \ReflectionClass($parent);
56 688
            } catch (\ReflectionException) {
57 688
                continue;
58
            }
59
60
            $ann = $this->reader->firstClassMetadata($class, Entity::class);
61 1032
            if ($ann !== null) {
62
                $parents[] = $class;
63
            }
64 1032
        }
65
66 1032
        return $parents;
67 996
    }
68 9
69 1032
    public function tableName(string $role, int $namingStrategy = Entities::TABLE_NAMING_PLURAL): string
70
    {
71
        return match ($namingStrategy) {
72
            Entities::TABLE_NAMING_PLURAL => $this->inflector->pluralize($this->inflector->tableize($role)),
73
            Entities::TABLE_NAMING_SINGULAR => $this->inflector->singularize($this->inflector->tableize($role)),
74
            default => $this->inflector->tableize($role),
75
        };
76
    }
77
}
78