| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of StaticReview | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright (c) 2014 Samuel Parkinson <@samparkinson_> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @see http://github.com/sjparkinson/static-review/blob/master/LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Padosoft\StaticReview\PHP; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use StaticReview\File\FileInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use StaticReview\Reporter\ReporterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use StaticReview\Review\AbstractFileReview; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use StaticReview\Review\ReviewableInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class PhpCodeSnifferReviewNoBlade extends AbstractFileReview | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $options = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * Gets the value of an option. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param  string $option | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function getOption($option) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         return $this->options[$option]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * Gets a string of the set options to pass to the command line. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function getOptionsForConsole() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $builder = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         foreach ($this->options as $option => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $builder .= '--' . $option; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             if ($value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 $builder .= '=' . $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $builder .= ' '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return $builder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * Adds an option to be included when running PHP_CodeSniffer. Overwrites the values of options with the same name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param  string               $option | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param  string               $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return PhpCodeSnifferReview | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function setOption($option, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if ($option === 'report') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             throw new \RuntimeException('"report" is not a valid option name.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $this->options[$option] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * Determins if a given file should be reviewed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param  FileInterface $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     public function canReviewFile(FileInterface $file) | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         return ($file->getExtension() === 'php' && substr($file->getFileName(),-strlen('blade.php'))!='blade.php'); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * Checks PHP files using PHP_CodeSniffer. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     public function review(ReporterInterface $reporter, ReviewableInterface $file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $bin = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, 'vendor/bin/phpcs'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $cmd = $bin . ' --report=json '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         if ($this->getOptionsForConsole()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             $cmd .= $this->getOptionsForConsole(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $cmd .= $file->getFullPath(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $process = $this->getProcess($cmd); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $process->run(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         if (! $process->isSuccessful()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             // Create the array of outputs and remove empty values. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             $output = json_decode($process->getOutput(), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $filter = function ($acc, $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 if ($file['errors'] > 0 || $file['warnings'] > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                     return $acc + $file['messages']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             foreach (array_reduce($output['files'], $filter, []) as $error) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 $message = $error['message'] . ' on line ' . $error['line']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 $reporter->warning($message, $this, $file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |  | 
            
                        
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: