Passed
Push — 3.x ( f6084f...6aff79 )
by Aleksei
04:21
created

EntityUtils   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 86.96%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 55
ccs 20
cts 23
cp 0.8696
rs 10
c 2
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hasParent() 0 3 1
A findParent() 0 25 6
A __construct() 0 3 1
A tableName() 0 6 1
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 984
    public function __construct(private ReaderInterface $reader)
20
    {
21 984
        $this->inflector = (new InflectorFactory())->build();
22 984
    }
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 960
    public function hasParent(string $class, bool $root = true): bool
28
    {
29 960
        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 960
    public function findParent(string $class, bool $root = true): ?string
38
    {
39
        /** @var class-string[] $parents */
40 960
        $parents = class_parents($class);
41
42 960
        $parents = $root ? array_reverse($parents) : $parents;
43
44 960
        foreach ($parents as $parent) {
45
            try {
46 616
                $class = new \ReflectionClass($parent);
47
            } catch (\ReflectionException) {
48
                continue;
49
            }
50
51 616
            if ($class->getDocComment() === false) {
52
                continue;
53
            }
54
55 616
            $ann = $this->reader->firstClassMetadata($class, Entity::class);
56 616
            if ($ann !== null) {
57 616
                return $parent;
58
            }
59
        }
60
61 960
        return null;
62
    }
63
64 960
    public function tableName(string $role, int $namingStrategy = Entities::TABLE_NAMING_PLURAL): string
65
    {
66 960
        return match ($namingStrategy) {
67 924
            Entities::TABLE_NAMING_PLURAL => $this->inflector->pluralize($this->inflector->tableize($role)),
68 9
            Entities::TABLE_NAMING_SINGULAR => $this->inflector->singularize($this->inflector->tableize($role)),
69 960
            default => $this->inflector->tableize($role),
70
        };
71
    }
72
}
73