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 |
||
30 | class UrlRule extends Object implements UrlRuleInterface |
||
31 | { |
||
32 | /** |
||
33 | * Set [[mode]] with this value to mark that this rule is for URL parsing only |
||
34 | */ |
||
35 | const PARSING_ONLY = 1; |
||
36 | /** |
||
37 | * Set [[mode]] with this value to mark that this rule is for URL creation only |
||
38 | */ |
||
39 | const CREATION_ONLY = 2; |
||
40 | |||
41 | /** |
||
42 | * @var string the name of this rule. If not set, it will use [[pattern]] as the name. |
||
43 | */ |
||
44 | public $name; |
||
45 | /** |
||
46 | * On the rule initialization, the [[pattern]] matching parameters names will be replaced with [[placeholders]]. |
||
47 | * @var string the pattern used to parse and create the path info part of a URL. |
||
48 | * @see host |
||
49 | * @see placeholders |
||
50 | */ |
||
51 | public $pattern; |
||
52 | /** |
||
53 | * @var string the pattern used to parse and create the host info part of a URL (e.g. `http://example.com`). |
||
54 | * @see pattern |
||
55 | */ |
||
56 | public $host; |
||
57 | /** |
||
58 | * @var string the route to the controller action |
||
59 | */ |
||
60 | public $route; |
||
61 | /** |
||
62 | * @var array the default GET parameters (name => value) that this rule provides. |
||
63 | * When this rule is used to parse the incoming request, the values declared in this property |
||
64 | * will be injected into $_GET. |
||
65 | */ |
||
66 | public $defaults = []; |
||
67 | /** |
||
68 | * @var string the URL suffix used for this rule. |
||
69 | * For example, ".html" can be used so that the URL looks like pointing to a static HTML page. |
||
70 | * If not set, the value of [[UrlManager::suffix]] will be used. |
||
71 | */ |
||
72 | public $suffix; |
||
73 | /** |
||
74 | * @var string|array the HTTP verb (e.g. GET, POST, DELETE) that this rule should match. |
||
75 | * Use array to represent multiple verbs that this rule may match. |
||
76 | * If this property is not set, the rule can match any verb. |
||
77 | * Note that this property is only used when parsing a request. It is ignored for URL creation. |
||
78 | */ |
||
79 | public $verb; |
||
80 | /** |
||
81 | * @var int a value indicating if this rule should be used for both request parsing and URL creation, |
||
82 | * parsing only, or creation only. |
||
83 | * If not set or 0, it means the rule is both request parsing and URL creation. |
||
84 | * If it is [[PARSING_ONLY]], the rule is for request parsing only. |
||
85 | * If it is [[CREATION_ONLY]], the rule is for URL creation only. |
||
86 | */ |
||
87 | public $mode; |
||
88 | /** |
||
89 | * @var bool a value indicating if parameters should be url encoded. |
||
90 | */ |
||
91 | public $encodeParams = true; |
||
92 | /** |
||
93 | * @var UrlNormalizer|array|false|null the configuration for [[UrlNormalizer]] used by this rule. |
||
94 | * If `null`, [[UrlManager::normalizer]] will be used, if `false`, normalization will be skipped |
||
95 | * for this rule. |
||
96 | * @since 2.0.10 |
||
97 | */ |
||
98 | public $normalizer; |
||
99 | |||
100 | /** |
||
101 | * @var array list of placeholders for matching parameters names. Used in [[parseRequest()]], [[createUrl()]]. |
||
102 | * On the rule initialization, the [[pattern]] parameters names will be replaced with placeholders. |
||
103 | * This array contains relations between the original parameters names and their placeholders. |
||
104 | * The array keys are the placeholders and the values are the original names. |
||
105 | * |
||
106 | * @see parseRequest() |
||
107 | * @see createUrl() |
||
108 | * @since 2.0.7 |
||
109 | */ |
||
110 | protected $placeholders = []; |
||
111 | |||
112 | /** |
||
113 | * @var string the template for generating a new URL. This is derived from [[pattern]] and is used in generating URL. |
||
114 | */ |
||
115 | private $_template; |
||
116 | /** |
||
117 | * @var string the regex for matching the route part. This is used in generating URL. |
||
118 | */ |
||
119 | private $_routeRule; |
||
120 | /** |
||
121 | * @var array list of regex for matching parameters. This is used in generating URL. |
||
122 | */ |
||
123 | private $_paramRules = []; |
||
124 | /** |
||
125 | * @var array list of parameters used in the route. |
||
126 | */ |
||
127 | private $_routeParams = []; |
||
128 | |||
129 | |||
130 | /** |
||
131 | * @return string |
||
132 | * @since 2.0.11 |
||
133 | */ |
||
134 | public function __toString() |
||
150 | |||
151 | /** |
||
152 | * Initializes this rule. |
||
153 | */ |
||
154 | public function init() |
||
263 | |||
264 | /** |
||
265 | * @param UrlManager $manager the URL manager |
||
266 | * @return UrlNormalizer|null |
||
267 | * @since 2.0.10 |
||
268 | */ |
||
269 | protected function getNormalizer($manager) |
||
277 | |||
278 | /** |
||
279 | * @param UrlManager $manager the URL manager |
||
280 | * @return bool |
||
281 | * @since 2.0.10 |
||
282 | */ |
||
283 | protected function hasNormalizer($manager) |
||
287 | |||
288 | /** |
||
289 | * Parses the given request and returns the corresponding route and parameters. |
||
290 | * @param UrlManager $manager the URL manager |
||
291 | * @param Request $request the request component |
||
292 | * @return array|bool the parsing result. The route and the parameters are returned as an array. |
||
293 | * If `false`, it means this rule cannot be used to parse this path info. |
||
294 | */ |
||
295 | public function parseRequest($manager, $request) |
||
363 | |||
364 | /** |
||
365 | * Creates a URL according to the given route and parameters. |
||
366 | * @param UrlManager $manager the URL manager |
||
367 | * @param string $route the route. It should not have slashes at the beginning or the end. |
||
368 | * @param array $params the parameters |
||
369 | * @return string|bool the created URL, or `false` if this rule cannot be used for creating this URL. |
||
370 | */ |
||
371 | public function createUrl($manager, $route, $params) |
||
450 | |||
451 | /** |
||
452 | * Returns list of regex for matching parameter. |
||
453 | * @return array parameter keys and regexp rules. |
||
454 | * |
||
455 | * @since 2.0.6 |
||
456 | */ |
||
457 | protected function getParamRules() |
||
461 | |||
462 | /** |
||
463 | * Iterates over [[placeholders]] and checks whether each placeholder exists as a key in $matches array. |
||
464 | * When found - replaces this placeholder key with a appropriate name of matching parameter. |
||
465 | * Used in [[parseRequest()]], [[createUrl()]]. |
||
466 | * |
||
467 | * @param array $matches result of `preg_match()` call |
||
468 | * @return array input array with replaced placeholder keys |
||
469 | * @see placeholders |
||
470 | * @since 2.0.7 |
||
471 | */ |
||
472 | protected function substitutePlaceholderNames(array $matches) |
||
482 | |||
483 | /** |
||
484 | * Trim slashes in passed string. If string begins with '//', two slashes are left as is |
||
485 | * in the beginning of a string. |
||
486 | * |
||
487 | * @param string $string |
||
488 | * @return string |
||
489 | */ |
||
490 | private function trimSlashes($string) { |
||
496 | } |
||
497 |