These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Hateoas\Factory; |
||
4 | |||
5 | use Hateoas\Configuration\Relation; |
||
6 | use Hateoas\Configuration\Route; |
||
7 | use Hateoas\Expression\ExpressionEvaluator; |
||
8 | use Hateoas\Model\Link; |
||
9 | use Hateoas\UrlGenerator\UrlGeneratorRegistry; |
||
10 | |||
11 | /** |
||
12 | * @author Adrien Brault <[email protected]> |
||
13 | */ |
||
14 | class LinkFactory |
||
15 | { |
||
16 | /** |
||
17 | * @var ExpressionEvaluator |
||
18 | */ |
||
19 | private $expressionEvaluator; |
||
20 | |||
21 | /** |
||
22 | * @var UrlGeneratorRegistry |
||
23 | */ |
||
24 | private $urlGeneratorRegistry; |
||
25 | |||
26 | /** |
||
27 | * @param ExpressionEvaluator $expressionEvaluator |
||
28 | * @param UrlGeneratorRegistry $urlGeneratorRegistry |
||
29 | */ |
||
30 | public function __construct(ExpressionEvaluator $expressionEvaluator, UrlGeneratorRegistry $urlGeneratorRegistry) |
||
31 | { |
||
32 | $this->expressionEvaluator = $expressionEvaluator; |
||
33 | $this->urlGeneratorRegistry = $urlGeneratorRegistry; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @param object $object |
||
38 | * @param Relation $relation |
||
39 | * |
||
40 | * @return Link |
||
41 | */ |
||
42 | public function createLink($object, Relation $relation) |
||
43 | { |
||
44 | $rel = $this->expressionEvaluator->evaluate($relation->getName(), $object); |
||
45 | $href = $relation->getHref(); |
||
46 | |||
47 | if ($href instanceof Route) { |
||
48 | if (!$this->urlGeneratorRegistry->hasGenerators()) { |
||
49 | throw new \RuntimeException('You cannot use a route without an url generator.'); |
||
50 | } |
||
51 | |||
52 | $name = $this->expressionEvaluator->evaluate($href->getName(), $object); |
||
53 | $parameters = is_array($href->getParameters()) |
||
54 | ? $this->expressionEvaluator->evaluateArray($href->getParameters(), $object) |
||
55 | : $this->expressionEvaluator->evaluate($href->getParameters(), $object) |
||
56 | ; |
||
57 | $isAbsolute = $this->expressionEvaluator->evaluate($href->isAbsolute(), $object); |
||
58 | |||
59 | if (!is_array($parameters)) { |
||
60 | throw new \RuntimeException( |
||
61 | sprintf( |
||
62 | 'The route parameters should be an array, %s given. Maybe you forgot to wrap the expression in expr(...).', |
||
63 | gettype($parameters) |
||
64 | ) |
||
65 | ); |
||
66 | } |
||
67 | |||
68 | $href = $this->urlGeneratorRegistry |
||
69 | ->get($href->getGenerator()) |
||
70 | ->generate($name, $parameters, $isAbsolute) |
||
0 ignored issues
–
show
|
|||
71 | ; |
||
72 | } else { |
||
73 | $href = $this->expressionEvaluator->evaluate($href, $object); |
||
74 | } |
||
75 | |||
76 | $attributes = $this->expressionEvaluator->evaluateArray($relation->getAttributes(), $object); |
||
77 | |||
78 | return new Link($rel, $href, $attributes); |
||
0 ignored issues
–
show
It seems like
$rel defined by $this->expressionEvaluat...on->getName(), $object) on line 44 can also be of type array ; however, Hateoas\Model\Link::__construct() does only seem to accept string , maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.
Loading history...
It seems like
$href defined by $this->expressionEvaluat...valuate($href, $object) on line 73 can also be of type array ; however, Hateoas\Model\Link::__construct() does only seem to accept string , maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.
Loading history...
|
|||
79 | } |
||
80 | } |
||
81 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.