• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nextras / orm / 4098113582

pending completion
4098113582

push

github

GitHub
Merge pull request #604 from nextras/fqn-table-names

37 of 43 new or added lines in 5 files covered. (86.05%)

6 existing lines in 5 files now uncovered.

3872 of 4198 relevant lines covered (92.23%)

1.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.73
/src/Entity/Reflection/MetadataValidator.php
1
<?php declare(strict_types = 1);
2

3
namespace Nextras\Orm\Entity\Reflection;
4

5

6
use Nextras\Orm\Exception\InvalidStateException;
7
use Nextras\Orm\Model\IRepositoryLoader;
8

9

10
class MetadataValidator
11
{
12
        /**
13
         * @param EntityMetadata[] $metadata
14
         * @throws InvalidStateException
15
         */
16
        public function validate(array $metadata, IRepositoryLoader $repositoryLoader): void
17
        {
18
                $pairs = [
2✔
19
                        PropertyRelationshipMetadata::MANY_HAS_MANY => PropertyRelationshipMetadata::MANY_HAS_MANY,
20
                        PropertyRelationshipMetadata::MANY_HAS_ONE => PropertyRelationshipMetadata::ONE_HAS_MANY,
21
                        PropertyRelationshipMetadata::ONE_HAS_MANY => PropertyRelationshipMetadata::MANY_HAS_ONE,
22
                        PropertyRelationshipMetadata::ONE_HAS_ONE => PropertyRelationshipMetadata::ONE_HAS_ONE,
23
                ];
24

25
                foreach ($metadata as $entityMeta) {
2✔
26
                        foreach ($entityMeta->getProperties() as $propertyMeta) {
2✔
27
                                if ($propertyMeta->relationship === null) {
2✔
28
                                        continue;
2✔
29
                                }
30

31
                                $repositoryName = $propertyMeta->relationship->repository;
2✔
32
                                if (!$repositoryLoader->hasRepository($repositoryName)) {
2✔
33
                                        throw new InvalidStateException("{$entityMeta->className}::\${$propertyMeta->name} points to unknown '{$propertyMeta->relationship->repository}' repository.");
×
34
                                }
35

36
                                if ($propertyMeta->relationship->property === null) {
2✔
37
                                        $relType = $propertyMeta->relationship->type;
2✔
38
                                        $isAllowedOneSided
2✔
39
                                                = ($relType === PropertyRelationshipMetadata::ONE_HAS_ONE && $propertyMeta->relationship->isMain)
2✔
40
                                                || ($relType === PropertyRelationshipMetadata::MANY_HAS_ONE)
2✔
UNCOV
41
                                                || ($relType === PropertyRelationshipMetadata::MANY_HAS_MANY && $propertyMeta->relationship->isMain);
×
42
                                        if (!$isAllowedOneSided) {
2✔
43
                                                throw new InvalidStateException("{$entityMeta->className}::\${$propertyMeta->name} must have defined a symmetric relationship.");
×
44
                                        } else {
45
                                                continue;
2✔
46
                                        }
47
                                }
48

49
                                $symmetricEntityMeta = $metadata[$propertyMeta->relationship->entity];
2✔
50

51
                                if (!$symmetricEntityMeta->hasProperty($propertyMeta->relationship->property)) {
2✔
52
                                        throw new InvalidStateException("{$entityMeta->className}::\${$propertyMeta->name} has not defined a symmetric relationship in {$symmetricEntityMeta->className}::\${$propertyMeta->relationship->property}.");
×
53
                                }
54

55
                                /** @var PropertyMetadata $symmetricPropertyMeta */
56
                                $symmetricPropertyMeta = $symmetricEntityMeta->getProperty($propertyMeta->relationship->property);
2✔
57
                                if ($symmetricPropertyMeta->relationship === null) {
2✔
58
                                        throw new InvalidStateException("{$entityMeta->className}::\${$propertyMeta->name} has not defined a symmetric relationship in {$symmetricEntityMeta->className}::\${$propertyMeta->relationship->property}.");
×
59
                                }
60

61
                                if ($propertyMeta->name !== $symmetricPropertyMeta->relationship->property) {
2✔
62
                                        throw new InvalidStateException("{$entityMeta->className}::\${$propertyMeta->name} relationship with {$symmetricEntityMeta->className}::\${$propertyMeta->relationship->property} is not symmetric.");
×
63
                                }
64

65
                                if ($symmetricPropertyMeta->relationship->type !== $pairs[$propertyMeta->relationship->type]) {
2✔
66
                                        throw new InvalidStateException("{$entityMeta->className}::\${$propertyMeta->name} has not defined a proper reverse relationship type in {$symmetricEntityMeta->className}::\${$propertyMeta->relationship->property}.");
×
67
                                }
68

69
                                if ($propertyMeta->relationship->type === PropertyRelationshipMetadata::MANY_HAS_MANY || $propertyMeta->relationship->type === PropertyRelationshipMetadata::ONE_HAS_ONE) {
2✔
70
                                        if ($propertyMeta->relationship->isMain && $symmetricPropertyMeta->relationship->isMain) {
2✔
71
                                                throw new InvalidStateException("Only one side of relationship {$entityMeta->className}::\${$propertyMeta->name} × {$symmetricEntityMeta->className}::\${$propertyMeta->relationship->property} could be defined as a main.");
×
72
                                        } elseif (!$propertyMeta->relationship->isMain && !$symmetricPropertyMeta->relationship->isMain) {
2✔
73
                                                throw new InvalidStateException("At least one side of relationship {$entityMeta->className}::\${$propertyMeta->name} × {$symmetricEntityMeta->className}::\${$propertyMeta->relationship->property} has to be defined as a main.");
×
74
                                        }
75
                                }
76
                        }
77
                }
78
        }
2✔
79
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc