| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the `liip/LiipImagineBundle` project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE.md | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Liip\ImagineBundle\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Imagine\Exception\RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Liip\ImagineBundle\Component\Console\Style\ImagineStyle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Liip\ImagineBundle\Imagine\Cache\CacheManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Liip\ImagineBundle\Imagine\Filter\FilterManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | trait CacheCommandTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var CacheManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $cacheManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var FilterManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $filterManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var ImagineStyle | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     private $io; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     private $outputMachineReadable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     private $failures = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     private function setupOutputStyle(InputInterface $input, OutputInterface $output): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $this->outputMachineReadable = $input->getOption('as-script'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $this->io = new ImagineStyle($input, $output, $this->outputMachineReadable ? false : !$input->getOption('no-colors')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @return array[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     private function resolveInputFiltersAndPaths(InputInterface $input): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $input->getArgument('path'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $this->normalizeFilterList($input->getOption('filter')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @param string[] $filters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @return string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     private function normalizeFilterList(array $filters): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         if (0 < count($filters)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             return $filters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         if (0 < count($filters = array_keys((array) $this->filterManager->getFilterConfiguration()->all()))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             return $filters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         throw new RuntimeException('No filters have been defined in the active configuration!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     private function outputCommandHeader(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         if (!$this->outputMachineReadable) { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |             $this->io->title($this->getName(), 'liip/imagine-bundle'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @param string[] $images | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @param string[] $filters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     private function outputCommandResult(array $images, array $filters, string $singularAction): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         if (!$this->outputMachineReadable) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $wordPluralizer = function (int $count, string $singular) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 return 1 === $count ? $singular : sprintf('%ss', $singular); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $rootTextFormat = 'Completed %d %s (%d %s, %d %s)'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $imagePathsSize = count($images); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $filterSetsSize = count($filters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             $allActionsSize = ($filterSetsSize * $imagePathsSize) - $this->failures; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             $allActionsWord = $wordPluralizer($allActionsSize, $singularAction); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $rootTextOutput = vsprintf($rootTextFormat, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 $allActionsSize, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 $allActionsWord, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 $imagePathsSize, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 $wordPluralizer($imagePathsSize, 'image'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                 $filterSetsSize, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 $wordPluralizer($filterSetsSize, 'filter'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             if ($this->failures) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 $this->io->critBlock(sprintf('%s %%s', $rootTextOutput), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                     sprintf('[encountered %d failures]', $this->failures) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 $this->io->okayBlock($rootTextOutput); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     private function getResultCode(): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         return 0 === $this->failures ? 0 : 255; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 132 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |  | 
            
                        
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.