| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace vipnytt\RobotsTxtParser\Directives; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use vipnytt\RobotsTxtParser\ObjectTools; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use vipnytt\RobotsTxtParser\RobotsTxtInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use vipnytt\RobotsTxtParser\UrlToolbox; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Class Disallow | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @package vipnytt\RobotsTxtParser\Directives | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 13 |  | View Code Duplication | class Disallow implements DirectiveInterface, RobotsTxtInterface | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     use UrlToolbox; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     use ObjectTools; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     const SUB_DIRECTIVES = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         self::DIRECTIVE_CLEAN_PARAM, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         self::DIRECTIVE_HOST, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * Directive | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     const DIRECTIVE = 'Disallow'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected $array = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $parent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     protected $cleanParam; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected $host; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function __construct($array, $parent = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->array = $array; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->cleanParam = new CleanParam([], self::DIRECTIVE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->host = new Host([], self::DIRECTIVE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * Add | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param string $line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     public function add($line) | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $pair = $this->generateRulePair($line, self::SUB_DIRECTIVES); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         switch ($pair['directive']) { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             case self::DIRECTIVE_CLEAN_PARAM: | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |                 return $this->cleanParam->add($pair['value']); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             case self::DIRECTIVE_HOST: | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                 return $this->host->add($pair['value']); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         return $this->addPath($line); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     protected function addPath($rule) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         // Return an array of paths | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         if (isset($this->array['path']) && in_array($rule, $this->array['path'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->array['path'][] = $rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * Check | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param  string $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     public function check($url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $path = $this->getPath($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         return ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             $this->checkPath($path, isset($this->array['path']) ? $this->array['path'] : []) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $this->cleanParam->check($path) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $this->host->check($url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function export() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $result = $this->array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             + $this->cleanParam->export() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             + $this->host->export(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         return empty($result) ? [] : [self::DIRECTIVE => $result]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.