1 | <?php |
||
35 | class ProvidersContainer |
||
36 | { |
||
37 | const PROVIDERS_NAMESPACE = 'seregazhuk\\PinterestBot\\Api\\Providers\\'; |
||
38 | |||
39 | /** |
||
40 | * References to the request that travels |
||
41 | * through the application. |
||
42 | * |
||
43 | * @var Request |
||
44 | */ |
||
45 | protected $request; |
||
46 | |||
47 | /** |
||
48 | * @var Response |
||
49 | */ |
||
50 | protected $response; |
||
51 | |||
52 | /** |
||
53 | * A array containing the cached providers. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $providers = []; |
||
58 | |||
59 | /** |
||
60 | * @param Request $request |
||
61 | * @param Response $response |
||
62 | */ |
||
63 | public function __construct(Request $request, Response $response) |
||
68 | |||
69 | |||
70 | /** |
||
71 | * Magic method to access different providers from the container. |
||
72 | * |
||
73 | * @param string $provider |
||
74 | * @return Provider |
||
75 | */ |
||
76 | public function __get($provider) |
||
80 | |||
81 | |||
82 | /** |
||
83 | * Gets provider object by name. If there is no such provider |
||
84 | * in providers array, it will try to create it, then save |
||
85 | * it, and then return. |
||
86 | * |
||
87 | * @param string $provider |
||
88 | * |
||
89 | * @throws WrongProvider |
||
90 | * |
||
91 | * @return Provider |
||
92 | */ |
||
93 | protected function getProvider($provider) |
||
105 | |||
106 | /** |
||
107 | * Creates provider by class name, and if success saves |
||
108 | * it to providers array. Provider class must exist in PROVIDERS_NAMESPACE. |
||
109 | * |
||
110 | * @param string $provider |
||
111 | * @throws WrongProvider |
||
112 | */ |
||
113 | protected function addProvider($provider) |
||
119 | |||
120 | /** |
||
121 | * Build Provider object. |
||
122 | * |
||
123 | * @param string $className |
||
124 | * @throws WrongProvider |
||
125 | * @return ProviderWrapper |
||
126 | */ |
||
127 | protected function buildProvider($className) |
||
133 | |||
134 | /** |
||
135 | * Proxies call to Request object and returns message from |
||
136 | * the error object. |
||
137 | * |
||
138 | * @return string|null |
||
139 | */ |
||
140 | public function getLastError() |
||
150 | |||
151 | /** |
||
152 | * Returns client context from Pinterest response. By default info returns from the last |
||
153 | * Pinterest response. If there was no response before or the argument $reload is |
||
154 | * true, we make a dummy request to the main page to update client context. |
||
155 | * |
||
156 | * @param bool $reload |
||
157 | * @return array|null |
||
158 | */ |
||
159 | public function getClientInfo($reload = false) |
||
169 | |||
170 | /** |
||
171 | * Returns HttpClient object for setting user-agent string or |
||
172 | * other CURL available options. |
||
173 | * |
||
174 | * @return HttpClient |
||
175 | */ |
||
176 | public function getHttpClient() |
||
180 | |||
181 | /** |
||
182 | * @param string $provider |
||
183 | * @return string |
||
184 | * @throws WrongProvider |
||
185 | */ |
||
186 | protected function resolveProviderClass($provider) |
||
196 | |||
197 | /** |
||
198 | * @param string $className |
||
199 | * @return bool |
||
200 | */ |
||
201 | protected function checkIsProviderClass($className) |
||
207 | |||
208 | /** |
||
209 | * @return Request |
||
210 | */ |
||
211 | public function getRequest() |
||
215 | |||
216 | /** |
||
217 | * @return Response |
||
218 | */ |
||
219 | public function getResponse() |
||
223 | |||
224 | /** |
||
225 | * Creates a timeout |
||
226 | * @param int $seconds |
||
227 | * @return $this |
||
228 | */ |
||
229 | public function wait($seconds = 1) |
||
235 | } |
||
236 |
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: