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 Onigoetz\Deployer\Configuration; |
||
4 | |||
5 | use Onigoetz\Deployer\Configuration\Containers\ExtendableConfigurationContainer; |
||
6 | use Onigoetz\Deployer\Configuration\Sources\Cloned; |
||
7 | use Onigoetz\Deployer\Configuration\Sources\Upload; |
||
8 | |||
9 | class Source extends ExtendableConfigurationContainer |
||
10 | { |
||
11 | /** |
||
12 | * Create a source with the right class |
||
13 | * |
||
14 | * @param string $name The source name |
||
15 | * @param array $data |
||
16 | * @param ConfigurationManager $manager |
||
17 | * @param Source $parent |
||
18 | * @throws \LogicException |
||
19 | * @return Cloned|Upload |
||
20 | */ |
||
21 | 135 | public static function make($name, array $data, ConfigurationManager $manager, Source $parent = null) |
|
22 | { |
||
23 | 135 | $strategy = static::findStrategy($name, $data, $manager, $parent); |
|
24 | |||
25 | switch ($strategy) { |
||
26 | 132 | case 'clone': |
|
27 | 96 | return new Cloned($name, $data, $manager, $parent); |
|
28 | break; |
||
29 | 27 | case 'upload': |
|
30 | 36 | return new Upload($name, $data, $manager, $parent); |
|
31 | break; |
||
32 | 2 | default: |
|
33 | 2 | } |
|
34 | |||
35 | 3 | throw new \LogicException("Unrecognized strategy '{$strategy}'"); |
|
36 | } |
||
37 | |||
38 | 135 | protected static function findStrategy($name, $data, ConfigurationManager $manager, Source $parent = null) |
|
39 | { |
||
40 | 135 | if (isset($data['strategy'])) { |
|
41 | 132 | return $data['strategy']; |
|
42 | } |
||
43 | |||
44 | 15 | if ($parent) { |
|
45 | 6 | return $parent->getStrategy(); |
|
46 | } |
||
47 | |||
48 | 12 | if (isset($data['extends']) && $manager->has('source', $data['extends'])) { |
|
49 | 9 | return $manager->get('source', $data['extends'])->getStrategy(); |
|
0 ignored issues
–
show
|
|||
50 | } |
||
51 | |||
52 | 3 | throw new \LogicException("Cannot find a valid strategy for the source '$name'"); |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | 48 | public function getContainerType() |
|
59 | { |
||
60 | 48 | return 'source'; |
|
61 | } |
||
62 | |||
63 | 12 | public function getStrategy() |
|
64 | { |
||
65 | 12 | return $this->getValueOrFail('strategy', "no 'strategy' specified for source '{$this->name}''"); |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * @throws \LogicException |
||
70 | * @return mixed |
||
71 | */ |
||
72 | 60 | public function getPath() |
|
73 | { |
||
74 | 60 | return $this->getValueOrFail('path', "no 'path' specified for source '{$this->name}''"); |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | 27 | public function checkValidity() |
|
81 | { |
||
82 | 27 | $this->getPath(); |
|
83 | |||
84 | 21 | return true; |
|
85 | } |
||
86 | } |
||
87 |
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 sub-classes 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 parent class: