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 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface as SymfonyUrlGeneratorInterface; |
||
11 | |||
12 | /** |
||
13 | * @author Adrien Brault <[email protected]> |
||
14 | */ |
||
15 | class LinkFactory |
||
16 | { |
||
17 | /** |
||
18 | * @var ExpressionEvaluator |
||
19 | */ |
||
20 | private $expressionEvaluator; |
||
21 | |||
22 | /** |
||
23 | * @var UrlGeneratorRegistry |
||
24 | */ |
||
25 | private $urlGeneratorRegistry; |
||
26 | |||
27 | /** |
||
28 | * @param ExpressionEvaluator $expressionEvaluator |
||
29 | * @param UrlGeneratorRegistry $urlGeneratorRegistry |
||
30 | */ |
||
31 | public function __construct(ExpressionEvaluator $expressionEvaluator, UrlGeneratorRegistry $urlGeneratorRegistry) |
||
32 | { |
||
33 | $this->expressionEvaluator = $expressionEvaluator; |
||
34 | $this->urlGeneratorRegistry = $urlGeneratorRegistry; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @param object $object |
||
39 | * @param Relation $relation |
||
40 | * |
||
41 | * @return Link |
||
42 | */ |
||
43 | public function createLink($object, Relation $relation) |
||
44 | { |
||
45 | $rel = $this->expressionEvaluator->evaluate($relation->getName(), $object); |
||
46 | $href = $relation->getHref(); |
||
47 | |||
48 | if ($href instanceof Route) { |
||
49 | if (!$this->urlGeneratorRegistry->hasGenerators()) { |
||
50 | throw new \RuntimeException('You cannot use a route without an url generator.'); |
||
51 | } |
||
52 | |||
53 | $name = $this->expressionEvaluator->evaluate($href->getName(), $object); |
||
54 | $parameters = is_array($href->getParameters()) |
||
55 | ? $this->expressionEvaluator->evaluateArray($href->getParameters(), $object) |
||
56 | : $this->expressionEvaluator->evaluate($href->getParameters(), $object) |
||
57 | ; |
||
58 | $isAbsolute = $this->expressionEvaluator->evaluate($href->isAbsolute(), $object) |
||
0 ignored issues
–
show
|
|||
59 | ? SymfonyUrlGeneratorInterface::ABSOLUTE_PATH |
||
60 | : SymfonyUrlGeneratorInterface::ABSOLUTE_URL |
||
61 | ; |
||
62 | |||
63 | if (!is_array($parameters)) { |
||
64 | throw new \RuntimeException( |
||
65 | sprintf( |
||
66 | 'The route parameters should be an array, %s given. Maybe you forgot to wrap the expression in expr(...).', |
||
67 | gettype($parameters) |
||
68 | ) |
||
69 | ); |
||
70 | } |
||
71 | |||
72 | $href = $this->urlGeneratorRegistry |
||
73 | ->get($href->getGenerator()) |
||
74 | ->generate($name, $parameters, $isAbsolute) |
||
0 ignored issues
–
show
It seems like
$name defined by $this->expressionEvaluat...ef->getName(), $object) on line 53 can also be of type array ; however, Hateoas\UrlGenerator\Url...orInterface::generate() 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...
|
|||
75 | ; |
||
76 | } else { |
||
77 | $href = $this->expressionEvaluator->evaluate($href, $object); |
||
78 | } |
||
79 | |||
80 | $attributes = $this->expressionEvaluator->evaluateArray($relation->getAttributes(), $object); |
||
81 | |||
82 | return new Link($rel, $href, $attributes); |
||
0 ignored issues
–
show
It seems like
$rel defined by $this->expressionEvaluat...on->getName(), $object) on line 45 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 77 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...
|
|||
83 | } |
||
84 | } |
||
85 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: