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

ipublikuj / doctrine-crud / 10244968780

05 Aug 2024 08:03AM UTC coverage: 31.907% (-22.9%) from 54.777%
10244968780

push

github

web-flow
Merge pull request #8 from ipublikuj/feature/throw-exception

Throw dbal exception

0 of 2 new or added lines in 2 files covered. (0.0%)

277 existing lines in 8 files now uncovered.

164 of 514 relevant lines covered (31.91%)

2.13 hits per line

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

14.29
/src/Helpers.php
1
<?php declare(strict_types = 1);
2

3
/**
4
 * Helpers.php
5
 *
6
 * @copyright      More in LICENSE.md
7
 * @license        https://www.ipublikuj.eu
8
 * @author         Adam Kadlec <adam.kadlec@ipublikuj.eu>
9
 * @package        iPublikuj:DoctrineCrud!
10
 * @subpackage     common
11
 * @since          1.0.0
12
 *
13
 * @date           29.01.14
14
 */
15

16
namespace IPub\DoctrineCrud;
17

18
use ReflectionException;
19
use ReflectionMethod;
20
use ReflectionParameter;
21
use function array_key_exists;
22
use function array_slice;
23
use function class_exists;
24
use function interface_exists;
25
use function is_object;
26
use function is_subclass_of;
27
use function method_exists;
28
use function sprintf;
29
use function strtolower;
30

31
/**
32
 * Doctrine CRUD helpers
33
 *
34
 * @package        iPublikuj:DoctrineCrud!
35
 * @subpackage     common
36
 *
37
 * @author         Adam Kadlec <adam.kadlec@ipublikuj.eu>
38
 */
39
class Helpers
40
{
41

42
        /**
43
         * This method was inspired by same method in Nette framework
44
         *
45
         * @param array<mixed> $arguments
46
         *
47
         * @return array<mixed>
48
         *
49
         * @throws Exceptions\EntityCreation
50
         *
51
         * @throws ReflectionException
52
         */
53
        public static function autowireArguments(ReflectionMethod $method, array $arguments): array
54
        {
55
                $optCount = 0;
3✔
56
                $num = -1;
3✔
57
                $res = [];
3✔
58
                $methodName = $method->getDeclaringClass()->getName() . '::' . $method->getName() . '()';
3✔
59

60
                foreach ($method->getParameters() as $subNum => $parameter) {
3✔
UNCOV
61
                        if (!$parameter->isVariadic() && array_key_exists($parameter->getName(), $arguments)) {
×
UNCOV
62
                                $res[$subNum] = $arguments[$parameter->getName()];
×
UNCOV
63
                                unset($arguments[$parameter->getName()], $arguments[$subNum]);
×
UNCOV
64
                                $optCount = 0;
×
65

UNCOV
66
                        } elseif (array_key_exists($subNum, $arguments)) {
×
UNCOV
67
                                $res[$subNum] = $arguments[$subNum];
×
UNCOV
68
                                unset($arguments[$subNum]);
×
UNCOV
69
                                $optCount = 0;
×
70

71
                        } else {
UNCOV
72
                                $class = self::getParameterType($parameter);
×
73

74
                                if (
75
                                        (
UNCOV
76
                                                $class !== null
×
UNCOV
77
                                                && $parameter->allowsNull()
×
78
                                        )
UNCOV
79
                                        || $parameter->isOptional()
×
UNCOV
80
                                        || $parameter->isDefaultValueAvailable()
×
81
                                ) {
82
                                        // !optional + defaultAvailable = func($a = null, $b) since 5.4.7
83
                                        // optional + !defaultAvailable = i.e. Exception::__construct, mysqli::mysqli, ...
UNCOV
84
                                        $res[$subNum] = $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null;
×
UNCOV
85
                                        $optCount++;
×
86

87
                                } else {
UNCOV
88
                                        if ($class !== null && (class_exists($class) || interface_exists($class))) {
×
UNCOV
89
                                                foreach ($arguments as $key => $argument) {
×
90
                                                        if (
UNCOV
91
                                                                is_object($argument)
×
92
                                                                && (
93
                                                                        // @phpstan-ignore-next-line
UNCOV
94
                                                                        is_subclass_of($argument, $class) || $argument::class === $class
×
95
                                                                )
96
                                                        ) {
UNCOV
97
                                                                $res[$subNum] = $argument;
×
UNCOV
98
                                                                unset($arguments[$key]);
×
UNCOV
99
                                                                $optCount = 0;
×
100

UNCOV
101
                                                                continue 2;
×
102
                                                        }
103
                                                }
104
                                        }
105

UNCOV
106
                                        throw new Exceptions\EntityCreation(
×
UNCOV
107
                                                $parameter->getName(),
×
UNCOV
108
                                                sprintf(
×
UNCOV
109
                                                        'Parameter %s in %s has no class type hint or default value, so its value must be specified.',
×
UNCOV
110
                                                        $methodName,
×
UNCOV
111
                                                        $parameter->getName(),
×
UNCOV
112
                                                ),
×
UNCOV
113
                                        );
×
114
                                }
115
                        }
116
                }
117

118
                // extra parameters
119
                while (array_key_exists(++$num, $arguments)) {
3✔
UNCOV
120
                        $res[$num] = $arguments[$num];
×
UNCOV
121
                        unset($arguments[$num]);
×
UNCOV
122
                        $optCount = 0;
×
123
                }
124

125
                return $optCount > 0 ? array_slice($res, 0, -$optCount) : $res;
3✔
126
        }
127

128
        public static function getParameterType(ReflectionParameter $param): string|null
129
        {
UNCOV
130
                if ($param->hasType()) {
×
UNCOV
131
                        $rt = $param->getType();
×
132

UNCOV
133
                        $type = $rt !== null && method_exists($rt, 'getName') ? $rt->getName() : null;
×
134

UNCOV
135
                        $rc = $param->getDeclaringClass();
×
136

UNCOV
137
                        if ($rc === null) {
×
UNCOV
138
                                return null;
×
139
                        }
140

UNCOV
141
                        return $type !== null && strtolower($type) === 'self' ? $rc->getName() : $type;
×
142
                }
143

UNCOV
144
                return null;
×
145
        }
146

147
}
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