1 | <?php |
||
13 | abstract class AbstractProvider implements ProviderInterface |
||
14 | { |
||
15 | /** |
||
16 | * @var ObjectManager |
||
17 | */ |
||
18 | protected $manager; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $dataClass; |
||
24 | |||
25 | /** |
||
26 | * @var RepositoryInterface |
||
27 | */ |
||
28 | protected $repository; |
||
29 | |||
30 | /** |
||
31 | * @var FactoryInterface |
||
32 | */ |
||
33 | protected $factory; |
||
34 | |||
35 | public function __construct( |
||
46 | |||
47 | /** |
||
48 | * @return ResourceInterface |
||
49 | */ |
||
50 | public function createNew() |
||
54 | |||
55 | /** |
||
56 | * @param ResourceInterface $resource |
||
57 | */ |
||
58 | public function persist(ResourceInterface $resource) |
||
62 | |||
63 | /** |
||
64 | * @param ResourceInterface|null $resource |
||
65 | */ |
||
66 | public function flush(ResourceInterface $resource = null) |
||
70 | |||
71 | /** |
||
72 | * @param ResourceInterface $resource |
||
73 | * @param bool $single |
||
74 | */ |
||
75 | public function save(ResourceInterface $resource, $single = false) |
||
80 | |||
81 | /** |
||
82 | * @param ResourceInterface $resource |
||
83 | */ |
||
84 | public function detach(ResourceInterface $resource) |
||
88 | |||
89 | /** |
||
90 | * @param ResourceInterface $resource |
||
91 | */ |
||
92 | public function remove(ResourceInterface $resource) |
||
96 | |||
97 | /** |
||
98 | * @param null $objectName |
||
99 | */ |
||
100 | public function clear($objectName = null) |
||
104 | |||
105 | /** |
||
106 | * @param $id |
||
107 | * |
||
108 | * @return ResourceInterface |
||
109 | */ |
||
110 | public function find($id) |
||
114 | |||
115 | /** |
||
116 | * @return ResourceInterface[] |
||
117 | */ |
||
118 | public function findAll() |
||
122 | |||
123 | /** |
||
124 | * @param array $criteria |
||
125 | * @param array|null $orderBy |
||
126 | * @param null $limit |
||
127 | * @param null $offset |
||
128 | * |
||
129 | * @return ResourceInterface[] |
||
130 | */ |
||
131 | public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
||
135 | |||
136 | /** |
||
137 | * @param array $criteria |
||
138 | * |
||
139 | * @return ResourceInterface[] |
||
140 | */ |
||
141 | public function findOneBy(array $criteria) |
||
145 | } |
||
146 |
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: