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

phpolar / model / 15338053003

30 May 2025 02:46AM UTC coverage: 90.391%. First build
15338053003

Pull #122

github

web-flow
Merge 2b12e33c7 into 548609d0b
Pull Request #122: 121 add json modelresolver

11 of 14 new or added lines in 6 files covered. (78.57%)

254 of 281 relevant lines covered (90.39%)

3.72 hits per line

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

100.0
/src/AbstractModelResolver.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Phpolar\Model;
6

7
use Phpolar\ModelResolver\ModelResolverInterface;
8
use ReflectionMethod;
9
use ReflectionNamedType;
10
use ReflectionParameter;
11
use RuntimeException;
12

13
/**
14
 * Converts an object that is marked as a model
15
 * attribute to a argument-name-object key-value pair.
16
 */
17
abstract class AbstractModelResolver implements ModelResolverInterface
18
{
19
    public function __construct(
20
        protected mixed $unresolvedObj,
21
    ) {
22
    }
12✔
23

24
    /**
25
     * Return the argument-name, object key-value pair
26
     * of the Model.
27
     *
28
     * @return array<string,AbstractModel>
29
     */
30
    public function resolve(object $obj, string $methodName): array
31
    {
32
        $reflectionMethod = new ReflectionMethod($obj, $methodName);
12✔
33
        $methodArgs = $reflectionMethod->getParameters();
12✔
34
        $modelParams = array_filter($methodArgs, $this->hasModelParams(...));
12✔
35
        $modelParamNames = array_map($this->getParamName(...), $modelParams);
12✔
36
        $modelInstances = array_map($this->newModel(...), $modelParams);
12✔
37
        return array_filter(
10✔
38
            array_combine(
10✔
39
                $modelParamNames,
10✔
40
                $modelInstances,
10✔
41
            )
10✔
42
        );
10✔
43
    }
44

45
    private function getParamName(ReflectionParameter $param): string
46
    {
47
        return $param->getName();
6✔
48
    }
49

50
    private function hasModelParams(ReflectionParameter $param): bool
51
    {
52
        return count($param->getAttributes(Model::class)) > 0;
12✔
53
    }
54

55
    private function newModel(ReflectionParameter $param): ?AbstractModel
56
    {
57
        $paramType = $param->getType();
6✔
58
        if ($paramType instanceof ReflectionNamedType) {
6✔
59
            $className = $paramType->getName();
6✔
60
            if (is_subclass_of($className, AbstractModel::class) === false) {
6✔
61
                throw new RuntimeException(
2✔
62
                    sprintf(
2✔
63
                        "Argument of type %s is not a subclass of %s",
2✔
64
                        $className,
2✔
65
                        AbstractModel::class
2✔
66
                    )
2✔
67
                );
2✔
68
            }
69
            return new $className($this->unresolvedObj);
4✔
70
        }
71
        return null; // @codeCoverageIgnore
72
    }
73
}
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