2929
3030/**
3131 * Class AbstractFactory
32+ *
33+ * @template T of AbstractModel
34+ *
3235 * @package Tmdb\Factory
3336 */
3437abstract class AbstractFactory
@@ -52,27 +55,28 @@ public function __construct(HttpClient $httpClient)
5255 * Convert an array to an hydrated object
5356 *
5457 * @param array $data
55- * @return AbstractModel
58+ * @return T
5659 */
5760 abstract public function create (array $ data = []);
5861
5962 /**
6063 * Convert an array with an collection of items to an hydrated object collection
6164 *
6265 * @param array $data
63- * @return GenericCollection
66+ * @return GenericCollection<T>
6467 */
6568 abstract public function createCollection (array $ data = []);
6669
6770 /**
6871 * Create a result collection
6972 *
70- * @param array $data
73+ * @param null| array $data
7174 * @param string $method
72- * @return ResultCollection
75+ * @return ResultCollection<T>
7376 */
7477 public function createResultCollection ($ data = [], $ method = 'create ' ): ResultCollection
7578 {
79+ /** @var ResultCollection<T> */
7680 $ collection = new ResultCollection ();
7781
7882 if (null === $ data ) {
@@ -106,7 +110,7 @@ public function createResultCollection($data = [], $method = 'create'): ResultCo
106110 * Create rating
107111 *
108112 * @param array $data
109- * @return AbstractModel
113+ * @return Rating
110114 */
111115 public function createRating (array $ data = [])
112116 {
@@ -116,9 +120,11 @@ public function createRating(array $data = [])
116120 /**
117121 * Hydrate the object with data
118122 *
119- * @param AbstractModel $subject
123+ * @template S of AbstractModel
124+ *
125+ * @param S $subject
120126 * @param array $data
121- * @return AbstractModel
127+ * @return S
122128 */
123129 protected function hydrate (AbstractModel $ subject , $ data = [])
124130 {
@@ -167,7 +173,7 @@ protected function getHttpClient()
167173 * Create the account states
168174 *
169175 * @param array $data
170- * @return AbstractModel
176+ * @return AccountStates
171177 */
172178 public function createAccountStates (array $ data = [])
173179 {
@@ -190,7 +196,7 @@ public function createAccountStates(array $data = [])
190196 * Create result
191197 *
192198 * @param array $data
193- * @return AbstractModel
199+ * @return Result
194200 */
195201 public function createResult (array $ data = [])
196202 {
@@ -200,12 +206,13 @@ public function createResult(array $data = [])
200206 /**
201207 * Create a generic collection of data and map it on the class by it's static parameter $properties
202208 *
209+ * @template S of AbstractModel
203210 * @param array $data
204- * @param AbstractModel $class
211+ * @param S|string $class
205212 *
206- * @return GenericCollection
213+ * @return GenericCollection<S>
207214 */
208- protected function createGenericCollection (array $ data = [], AbstractModel $ class = null ): GenericCollection
215+ protected function createGenericCollection (array $ data = [], $ class = null ): GenericCollection
209216 {
210217 if (!$ class ) {
211218 throw new \Tmdb \Exception \RuntimeException ('Expected a class to be present. ' );
@@ -215,12 +222,9 @@ protected function createGenericCollection(array $data = [], AbstractModel $clas
215222 $ class = get_class ($ class );
216223 }
217224
225+ /** @var GenericCollection<S> */
218226 $ collection = new GenericCollection ();
219227
220- if (null === $ data ) {
221- return $ collection ;
222- }
223-
224228 foreach ($ data as $ item ) {
225229 $ collection ->add (null , $ this ->hydrate (new $ class (), $ item ));
226230 }
@@ -231,28 +235,26 @@ protected function createGenericCollection(array $data = [], AbstractModel $clas
231235 /**
232236 * Create a generic collection of data and map it on the class by it's static parameter $properties
233237 *
238+ * @template S of AbstractModel
239+ * @template SC of GenericCollection<S>
234240 * @param array $data
235- * @param AbstractModel $class
236- * @param GenericCollection $collection
237- * @return GenericCollection
241+ * @param S|string $class
242+ * @param SC $collection
243+ * @return SC
238244 */
239245 protected function createCustomCollection (
240246 array $ data ,
241- AbstractModel $ class ,
247+ $ class ,
242248 GenericCollection $ collection
243249 ) {
244- if (!$ class || ! $ collection ) {
245- throw new \Tmdb \Exception \RuntimeException ('Expected both an class and collection to be given . ' );
250+ if (!$ class ) {
251+ throw new \Tmdb \Exception \RuntimeException ('Expected a class to be present . ' );
246252 }
247253
248254 if (is_object ($ class )) {
249255 $ class = get_class ($ class );
250256 }
251257
252- if (null === $ data ) {
253- return $ collection ;
254- }
255-
256258 foreach ($ data as $ item ) {
257259 $ collection ->add (null , $ this ->hydrate (new $ class (), $ item ));
258260 }
@@ -264,7 +266,7 @@ protected function createCustomCollection(
264266 * Create an generic collection of an array that consists out of a mix of movies and tv shows
265267 *
266268 * @param array $data
267- * @return GenericCollection
269+ * @return GenericCollection<AbstractModel>
268270 */
269271 protected function createGenericCollectionFromMediaTypes ($ data = [])
270272 {
0 commit comments