| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Pitchart\Collection\Helpers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Pitchart\Collection\CollectionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Pitchart\Collection\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Pitchart\Collection\GeneratorCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Get a Collection from an iterable list | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @param  iterable $items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @return Collection | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | function collect($items) | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 | 1 |  |     return Collection::from($items); | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Get a Collection from an iterable list | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @param  iterable $items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @return GeneratorCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | function generator($items) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 1 |  |     return GeneratorCollection::from($items); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * Applies a callable to the elements of an iterable list and returns a Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * @param  iterable $items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  * @param  callable $callable The callable takes an element as parameter and returns a transformed result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  * @return CollectionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | function map($items, $callable) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 6 |  |     if (is_array($items) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 4 |  |         || !($items instanceof CollectionInterface) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 6 |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 4 |  |         $items = Collection::from($items); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 6 |  |     return $items->map($callable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  * Filters elements of an iterable list and returns a Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  * @param  iterable $items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  * @param  callable $callable The callable takes an element as parameter and returns a boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  * @return CollectionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | function filter($items, $callable) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 6 |  |     if (is_array($items) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 4 |  |         || !($items instanceof CollectionInterface) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 6 |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 4 |  |         $items = Collection::from($items); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 6 |  |     return $items->filter($callable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  * Rejects elements of an iterable list and returns a Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  * @param  iterable $items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  * @param  callable $callable The callable takes an element as parameter and returns a boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  * @return CollectionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | function reject($items, $callable) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 6 |  |     if (is_array($items) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 4 |  |         || !($items instanceof CollectionInterface) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 6 |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 4 |  |         $items = Collection::from($items); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 6 |  |     return $items->reject($callable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  * Merge one or more iterables and returns a collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  * @param iterable $iterables | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  * @return CollectionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | function merge(...$iterables) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 4 |  |     foreach ($iterables as $position => $iterable) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 4 |  |         if (is_array($iterable) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 3 |  |             || !($iterable instanceof CollectionInterface) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 4 |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 4 |  |             $iterables[$position] = Collection::from($iterable); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 4 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** @var CollectionInterface $first */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 4 |  |     $first = array_shift($iterables); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 4 |  |     return $first->merge(...$iterables); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  * Iteratively reduce an iterable to a single value using a callable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  * @param callable $callable The callable takes an accumulator and an iterable element as parameters and returns a values which will be the accumulator of the next iteration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  * @param mixed    $initial | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | function reduce($items, $callable, $initial) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 6 |  |     if (is_array($items) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 4 |  |         || !($items instanceof CollectionInterface) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 6 |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 4 |  |         $items = Collection::from($items); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 6 |  |     return $items->reduce($callable, $initial); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 124 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |  | 
            
                        
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.