Conditions | 3 |
Paths | 4 |
Total Lines | 37 |
Code Lines | 20 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
16 | public static function formatForAnnotation(AnnotationInterface $annotation, $class) |
||
17 | { |
||
18 | $shortName = (new ReflectionClass($annotation))->getShortName(); |
||
19 | $type = preg_replace('~Annotation$~', '', $shortName); |
||
20 | $typeName = $annotation->getMeta()->type()->name; |
||
|
|||
21 | $name = $annotation->name; |
||
22 | |||
23 | // Always available params |
||
24 | $params = [ |
||
25 | $type, |
||
26 | $typeName, |
||
27 | ]; |
||
28 | |||
29 | // Prepare first part of message |
||
30 | if (empty($class)) |
||
31 | { |
||
32 | $msg = 'Could not resolve class name'; |
||
33 | } |
||
34 | else |
||
35 | { |
||
36 | array_unshift($params, $class); |
||
37 | $msg = 'Class `%s` not found'; |
||
38 | } |
||
39 | |||
40 | // Prepare location of bogus annotation |
||
41 | if ($name === $typeName) |
||
42 | { |
||
43 | $msgOn = 'for `@%s` annotation on `%s`'; |
||
44 | } |
||
45 | else |
||
46 | { |
||
47 | $params[] = $name; |
||
48 | $msgOn = 'for `@%s` annotation on `%s::%s`'; |
||
49 | } |
||
50 | $msg = implode(' ', [$msg, $msgOn]); |
||
51 | return vsprintf($msg, $params); |
||
52 | } |
||
53 | |||
55 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: