| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author Boudewijn Schoon <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright Zicht Online <http://zicht.nl> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | namespace Zicht\Itertools\lib; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Zicht\Itertools\lib\Interfaces\FiniteIterableInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Zicht\Itertools\lib\Traits\FiniteIterableTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Class MapIterator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @package Zicht\Itertools\lib | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class MapIterator extends \MultipleIterator implements FiniteIterableInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     use FiniteIterableTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** @var \Closure */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $valueFunc; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** @var \Closure */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $keyFunc; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * MapIterator constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param \Closure $valueFunc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 72 |  |     public function __construct(\Closure $valueFunc /* [\Closure $keyFunc], \Iterator $iterable1, [\Iterator $iterable2, [...]] */) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 72 |  |         parent::__construct(\MultipleIterator::MIT_NEED_ALL | \MultipleIterator::MIT_KEYS_NUMERIC); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 72 |  |         $args = func_get_args(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 72 |  |         $argsContainsKeyFunc = $args[1] instanceof \Closure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 72 |  |         $this->valueFunc = $args[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 72 |  |         if ($argsContainsKeyFunc) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |             $this->keyFunc = $args[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             $this->keyFunc = function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 65 |  |                 return $this->genericKeysToKey(func_get_args()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 72 |  |         foreach (array_slice($args, $argsContainsKeyFunc ? 2 : 1) as $iterable) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 72 |  |             if (!$iterable instanceof \Iterator) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |                 throw new \InvalidArgumentException(sprintf('Not all arguments are iterators')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 71 |  |             $this->attachIterator($iterable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 71 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * Compute the key for an element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * When multiple iterables are given, the key for an element is computed by taking | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * the keys of the element of all iterables, when they are the same, this key is used, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * otherwise a compound key is created by concatenating the keys together. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param array $keysAndValues | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 65 |  |     protected function genericKeysToKey($keysAndValues) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 65 |  |         $keys = array_splice($keysAndValues, 0, count($keysAndValues) / 2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 65 |  |         if (count($keys) == 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 52 |  |             return $keys[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 13 |  |         $value = $keys[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 13 |  |         foreach ($keys as $key) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 13 |  |             if ($key !== $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 // the keys are different, we will make a new string identifying this entry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 5 |  |                 return join( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 5 |  |                     ':', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                     array_map( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 13 |  |                         function ($key) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 5 |  |                             return (string)$key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 13 |  |                         }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                         $keys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         // all values are the same, use it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 9 |  |         return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @{inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 66 |  |     public function current() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 66 |  |         return call_user_func_array($this->valueFunc, array_merge(parent::current(), parent::key())); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @{inheritDoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 104 | 66 |  |     public function key() | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 106 | 66 |  |         return call_user_func_array($this->keyFunc, array_merge(parent::key(), parent::current())); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @{inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 66 |  |     public function next() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 66 |  |         parent::next(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 66 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |  | 
            
                        
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.