Complex classes like UrlRule often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UrlRule, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class UrlRule extends Object implements UrlRuleInterface |
||
34 | { |
||
35 | /** |
||
36 | * Set [[mode]] with this value to mark that this rule is for URL parsing only |
||
37 | */ |
||
38 | const PARSING_ONLY = 1; |
||
39 | /** |
||
40 | * Set [[mode]] with this value to mark that this rule is for URL creation only |
||
41 | */ |
||
42 | const CREATION_ONLY = 2; |
||
43 | /** |
||
44 | * Represents the successful URL generation by last [[createUrl()]] call. |
||
45 | * @see $createStatus |
||
46 | * @since 2.0.12 |
||
47 | */ |
||
48 | const CREATE_STATUS_SUCCESS = 0; |
||
49 | /** |
||
50 | * Represents the unsuccessful URL generation by last [[createUrl()]] call, because rule does not support |
||
51 | * creating URLs. |
||
52 | * @see $createStatus |
||
53 | * @since 2.0.12 |
||
54 | */ |
||
55 | const CREATE_STATUS_PARSING_ONLY = 1; |
||
56 | /** |
||
57 | * Represents the unsuccessful URL generation by last [[createUrl()]] call, because of mismatched route. |
||
58 | * @see $createStatus |
||
59 | * @since 2.0.12 |
||
60 | */ |
||
61 | const CREATE_STATUS_ROUTE_MISMATCH = 2; |
||
62 | /** |
||
63 | * Represents the unsuccessful URL generation by last [[createUrl()]] call, because of mismatched |
||
64 | * or missing parameters. |
||
65 | * @see $createStatus |
||
66 | * @since 2.0.12 |
||
67 | */ |
||
68 | const CREATE_STATUS_PARAMS_MISMATCH = 4; |
||
69 | |||
70 | /** |
||
71 | * @var string the name of this rule. If not set, it will use [[pattern]] as the name. |
||
72 | */ |
||
73 | public $name; |
||
74 | /** |
||
75 | * On the rule initialization, the [[pattern]] matching parameters names will be replaced with [[placeholders]]. |
||
76 | * @var string the pattern used to parse and create the path info part of a URL. |
||
77 | * @see host |
||
78 | * @see placeholders |
||
79 | */ |
||
80 | public $pattern; |
||
81 | /** |
||
82 | * @var string the pattern used to parse and create the host info part of a URL (e.g. `http://example.com`). |
||
83 | * @see pattern |
||
84 | */ |
||
85 | public $host; |
||
86 | /** |
||
87 | * @var string the route to the controller action |
||
88 | */ |
||
89 | public $route; |
||
90 | /** |
||
91 | * @var array the default GET parameters (name => value) that this rule provides. |
||
92 | * When this rule is used to parse the incoming request, the values declared in this property |
||
93 | * will be injected into $_GET. |
||
94 | */ |
||
95 | public $defaults = []; |
||
96 | /** |
||
97 | * @var string the URL suffix used for this rule. |
||
98 | * For example, ".html" can be used so that the URL looks like pointing to a static HTML page. |
||
99 | * If not set, the value of [[UrlManager::suffix]] will be used. |
||
100 | */ |
||
101 | public $suffix; |
||
102 | /** |
||
103 | * @var string|array the HTTP verb (e.g. GET, POST, DELETE) that this rule should match. |
||
104 | * Use array to represent multiple verbs that this rule may match. |
||
105 | * If this property is not set, the rule can match any verb. |
||
106 | * Note that this property is only used when parsing a request. It is ignored for URL creation. |
||
107 | */ |
||
108 | public $verb; |
||
109 | /** |
||
110 | * @var int a value indicating if this rule should be used for both request parsing and URL creation, |
||
111 | * parsing only, or creation only. |
||
112 | * If not set or 0, it means the rule is both request parsing and URL creation. |
||
113 | * If it is [[PARSING_ONLY]], the rule is for request parsing only. |
||
114 | * If it is [[CREATION_ONLY]], the rule is for URL creation only. |
||
115 | */ |
||
116 | public $mode; |
||
117 | /** |
||
118 | * @var bool a value indicating if parameters should be url encoded. |
||
119 | */ |
||
120 | public $encodeParams = true; |
||
121 | /** |
||
122 | * @var UrlNormalizer|array|false|null the configuration for [[UrlNormalizer]] used by this rule. |
||
123 | * If `null`, [[UrlManager::normalizer]] will be used, if `false`, normalization will be skipped |
||
124 | * for this rule. |
||
125 | * @since 2.0.10 |
||
126 | */ |
||
127 | public $normalizer; |
||
128 | |||
129 | /** |
||
130 | * @var int|null status of the URL creation after the last [[createUrl()]] call. |
||
131 | * @since 2.0.12 |
||
132 | */ |
||
133 | protected $createStatus; |
||
134 | /** |
||
135 | * @var array list of placeholders for matching parameters names. Used in [[parseRequest()]], [[createUrl()]]. |
||
136 | * On the rule initialization, the [[pattern]] parameters names will be replaced with placeholders. |
||
137 | * This array contains relations between the original parameters names and their placeholders. |
||
138 | * The array keys are the placeholders and the values are the original names. |
||
139 | * |
||
140 | * @see parseRequest() |
||
141 | * @see createUrl() |
||
142 | * @since 2.0.7 |
||
143 | */ |
||
144 | protected $placeholders = []; |
||
145 | |||
146 | /** |
||
147 | * @var string the template for generating a new URL. This is derived from [[pattern]] and is used in generating URL. |
||
148 | */ |
||
149 | private $_template; |
||
150 | /** |
||
151 | * @var string the regex for matching the route part. This is used in generating URL. |
||
152 | */ |
||
153 | private $_routeRule; |
||
154 | /** |
||
155 | * @var array list of regex for matching parameters. This is used in generating URL. |
||
156 | */ |
||
157 | private $_paramRules = []; |
||
158 | /** |
||
159 | * @var array list of parameters used in the route. |
||
160 | */ |
||
161 | private $_routeParams = []; |
||
162 | |||
163 | |||
164 | /** |
||
165 | * @return string |
||
166 | * @since 2.0.11 |
||
167 | */ |
||
168 | 12 | public function __toString() |
|
184 | |||
185 | /** |
||
186 | * Initializes this rule. |
||
187 | */ |
||
188 | 94 | public function init() |
|
218 | |||
219 | /** |
||
220 | * Process [[$pattern]] on rule initialization. |
||
221 | */ |
||
222 | 94 | private function preparePattern() |
|
259 | |||
260 | /** |
||
261 | * Prepares [[$pattern]] on rule initialization - replace parameter names by placeholders. |
||
262 | * |
||
263 | * @param bool $allowAppendSlash Defines position of slash in the param pattern in [[$pattern]]. |
||
264 | * If `false` slash will be placed at the beginning of param pattern. If `true` slash position will be detected |
||
265 | * depending on non-optional pattern part. |
||
266 | */ |
||
267 | 86 | private function translatePattern($allowAppendSlash) |
|
346 | |||
347 | /** |
||
348 | * @param UrlManager $manager the URL manager |
||
349 | * @return UrlNormalizer|null |
||
350 | * @since 2.0.10 |
||
351 | */ |
||
352 | 15 | protected function getNormalizer($manager) |
|
360 | |||
361 | /** |
||
362 | * @param UrlManager $manager the URL manager |
||
363 | * @return bool |
||
364 | * @since 2.0.10 |
||
365 | */ |
||
366 | 15 | protected function hasNormalizer($manager) |
|
370 | |||
371 | /** |
||
372 | * Parses the given request and returns the corresponding route and parameters. |
||
373 | * @param UrlManager $manager the URL manager |
||
374 | * @param Request $request the request component |
||
375 | * @return array|bool the parsing result. The route and the parameters are returned as an array. |
||
376 | * If `false`, it means this rule cannot be used to parse this path info. |
||
377 | */ |
||
378 | 15 | public function parseRequest($manager, $request) |
|
446 | |||
447 | /** |
||
448 | * Creates a URL according to the given route and parameters. |
||
449 | * @param UrlManager $manager the URL manager |
||
450 | * @param string $route the route. It should not have slashes at the beginning or the end. |
||
451 | * @param array $params the parameters |
||
452 | * @return string|bool the created URL, or `false` if this rule cannot be used for creating this URL. |
||
453 | */ |
||
454 | 78 | public function createUrl($manager, $route, $params) |
|
539 | |||
540 | /** |
||
541 | * Returns status of the URL creation after the last [[createUrl()]] call. |
||
542 | * |
||
543 | * @return null|int Status of the URL creation after the last [[createUrl()]] call. `null` if rule does not provide |
||
544 | * info about create status. |
||
545 | * @see $createStatus |
||
546 | * @since 2.0.12 |
||
547 | */ |
||
548 | 77 | public function getCreateUrlStatus() { |
|
551 | |||
552 | /** |
||
553 | * Returns list of regex for matching parameter. |
||
554 | * @return array parameter keys and regexp rules. |
||
555 | * |
||
556 | * @since 2.0.6 |
||
557 | */ |
||
558 | protected function getParamRules() |
||
562 | |||
563 | /** |
||
564 | * Iterates over [[placeholders]] and checks whether each placeholder exists as a key in $matches array. |
||
565 | * When found - replaces this placeholder key with a appropriate name of matching parameter. |
||
566 | * Used in [[parseRequest()]], [[createUrl()]]. |
||
567 | * |
||
568 | * @param array $matches result of `preg_match()` call |
||
569 | * @return array input array with replaced placeholder keys |
||
570 | * @see placeholders |
||
571 | * @since 2.0.7 |
||
572 | */ |
||
573 | 27 | protected function substitutePlaceholderNames(array $matches) |
|
583 | |||
584 | /** |
||
585 | * Trim slashes in passed string. If string begins with '//', two slashes are left as is |
||
586 | * in the beginning of a string. |
||
587 | * |
||
588 | * @param string $string |
||
589 | * @return string |
||
590 | */ |
||
591 | 94 | private function trimSlashes($string) |
|
598 | } |
||
599 |