| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of Aura for PHP. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @license http://opensource.org/licenses/bsd-license.php BSD | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace Aura\Router\Rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Aura\Router\Route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Psr\Http\Message\ServerRequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * A rule for the HTTP host. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @package Aura.Router | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class Host implements RuleInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * Use this Route to build the regex. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var Route | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     protected $route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * The regular expression for the host. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected $regex; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * Checks that the Request host matches the Route host. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param ServerRequestInterface $request The HTTP request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param Route $route The route. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @return bool True on success, false on failure. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 7 |  |     public function __invoke(ServerRequestInterface $request, Route $route) | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 7 |  |         if (! $route->host) { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 5 |  |             return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 2 |  |         $match = preg_match( | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 2 |  |             $this->buildRegex($route), | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 2 |  |             $request->getUri()->getHost(), | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             $matches | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 2 |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 2 |  |         if (! $match) { | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 1 |  |             return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 2 |  |         $route->attributes($this->getAttributes($matches)); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 2 |  |         return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * Gets the attributes out of the regex matches. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param array $matches The regex matches. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 2 |  |     protected function getAttributes($matches) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 2 |  |         $attributes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 2 |  |         foreach ($matches as $key => $val) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 2 |  |             if (is_string($key)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |                 $attributes[$key] = $val; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 2 |  |         return $attributes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * Builds the regular expression for the route host. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @param Route $route The Route. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 2 |  |     protected function buildRegex(Route $route) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 2 |  |         $this->route = $route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 2 |  |         $this->regex = str_replace('.', '\\.', $this->route->host); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 2 |  |         $this->setRegexAttributes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 2 |  |         $this->regex = '#^' . $this->regex . '$#'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 2 |  |         return $this->regex; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * Expands attribute names in the regex to named subpatterns; adds default | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * `null` values for attributes without defaults. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @return null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 2 |  |     protected function setRegexAttributes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 2 |  |         $find = '#{([a-z][a-zA-Z0-9_]*)}#'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 2 |  |         $attributes = $this->route->attributes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 2 |  |         $newAttributes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 2 |  |         preg_match_all($find, $this->regex, $matches, PREG_SET_ORDER); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 2 |  |         foreach ($matches as $match) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 1 |  |             $name = $match[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 1 |  |             $subpattern = $this->getSubpattern($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 1 |  |             $this->regex = str_replace("{{$name}}", $subpattern, $this->regex); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 1 |  |             if (! isset($attributes[$name])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 1 |  |                 $newAttributes[$name] = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 1 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 2 |  |         $this->route->attributes($newAttributes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * Returns a named subpattern for a attribute name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @param string $name The attribute name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @return string The named subpattern. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 1 |  |     protected function getSubpattern($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         // is there a custom subpattern for the name? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 1 |  |         if (isset($this->route->tokens[$name])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 1 |  |             return "(?P<{$name}>{$this->route->tokens[$name]})"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         // use a default subpattern, stop at first dot | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 1 |  |         return "(?P<{$name}>[^\.]+)"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 154 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 155 |  |  |  | 
            
                        
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.