Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like MediaWikiTitleCodec 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 MediaWikiTitleCodec, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class MediaWikiTitleCodec implements TitleFormatter, TitleParser { |
||
38 | /** |
||
39 | * @var Language |
||
40 | */ |
||
41 | protected $language; |
||
42 | |||
43 | /** |
||
44 | * @var GenderCache |
||
45 | */ |
||
46 | protected $genderCache; |
||
47 | |||
48 | /** |
||
49 | * @var string[] |
||
50 | */ |
||
51 | protected $localInterwikis; |
||
52 | |||
53 | /** |
||
54 | * @param Language $language The language object to use for localizing namespace names. |
||
55 | * @param GenderCache $genderCache The gender cache for generating gendered namespace names |
||
56 | * @param string[]|string $localInterwikis |
||
57 | */ |
||
58 | public function __construct( Language $language, GenderCache $genderCache, |
||
65 | |||
66 | /** |
||
67 | * @see TitleFormatter::getNamespaceName() |
||
68 | * |
||
69 | * @param int $namespace |
||
70 | * @param string $text |
||
71 | * |
||
72 | * @throws InvalidArgumentException If the namespace is invalid |
||
73 | * @return string |
||
74 | */ |
||
75 | public function getNamespaceName( $namespace, $text ) { |
||
93 | |||
94 | /** |
||
95 | * @see TitleFormatter::formatTitle() |
||
96 | * |
||
97 | * @param int|bool $namespace The namespace ID (or false, if the namespace should be ignored) |
||
98 | * @param string $text The page title. Should be valid. Only minimal normalization is applied. |
||
99 | * Underscores will be replaced. |
||
100 | * @param string $fragment The fragment name (may be empty). |
||
101 | * @param string $interwiki The interwiki name (may be empty). |
||
102 | * |
||
103 | * @throws InvalidArgumentException If the namespace is invalid |
||
104 | * @return string |
||
105 | */ |
||
106 | public function formatTitle( $namespace, $text, $fragment = '', $interwiki = '' ) { |
||
107 | if ( $namespace !== false ) { |
||
108 | $namespace = $this->getNamespaceName( $namespace, $text ); |
||
|
|||
109 | |||
110 | if ( $namespace !== '' ) { |
||
111 | $text = $namespace . ':' . $text; |
||
112 | } |
||
113 | } |
||
114 | |||
115 | if ( $fragment !== '' ) { |
||
116 | $text = $text . '#' . $fragment; |
||
117 | } |
||
118 | |||
119 | if ( $interwiki !== '' ) { |
||
120 | $text = $interwiki . ':' . $text; |
||
121 | } |
||
122 | |||
123 | $text = str_replace( '_', ' ', $text ); |
||
124 | |||
125 | return $text; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Parses the given text and constructs a TitleValue. Normalization |
||
130 | * is applied according to the rules appropriate for the form specified by $form. |
||
131 | * |
||
132 | * @param string $text The text to parse |
||
133 | * @param int $defaultNamespace Namespace to assume per default (usually NS_MAIN) |
||
134 | * |
||
135 | * @throws MalformedTitleException |
||
136 | * @return TitleValue |
||
137 | */ |
||
138 | public function parseTitle( $text, $defaultNamespace ) { |
||
139 | // NOTE: this is an ugly cludge that allows this class to share the |
||
140 | // code for parsing with the old Title class. The parser code should |
||
141 | // be refactored to avoid this. |
||
142 | $parts = $this->splitTitleString( $text, $defaultNamespace ); |
||
143 | |||
144 | // Relative fragment links are not supported by TitleValue |
||
145 | if ( $parts['dbkey'] === '' ) { |
||
146 | throw new MalformedTitleException( 'title-invalid-empty', $text ); |
||
147 | } |
||
148 | |||
149 | return new TitleValue( |
||
150 | $parts['namespace'], |
||
151 | $parts['dbkey'], |
||
152 | $parts['fragment'], |
||
153 | $parts['interwiki'] |
||
154 | ); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @see TitleFormatter::getText() |
||
159 | * |
||
160 | * @param LinkTarget $title |
||
161 | * |
||
162 | * @return string $title->getText() |
||
163 | */ |
||
164 | public function getText( LinkTarget $title ) { |
||
167 | |||
168 | /** |
||
169 | * @see TitleFormatter::getText() |
||
170 | * |
||
171 | * @param LinkTarget $title |
||
172 | * |
||
173 | * @return string |
||
174 | */ |
||
175 | public function getPrefixedText( LinkTarget $title ) { |
||
183 | |||
184 | /** |
||
185 | * @since 1.27 |
||
186 | * @see TitleFormatter::getPrefixedDBkey() |
||
187 | * @param LinkTarget $target |
||
188 | * @return string |
||
189 | */ |
||
190 | public function getPrefixedDBkey( LinkTarget $target ) { |
||
214 | |||
215 | /** |
||
216 | * @see TitleFormatter::getText() |
||
217 | * |
||
218 | * @param LinkTarget $title |
||
219 | * |
||
220 | * @return string |
||
221 | */ |
||
222 | public function getFullText( LinkTarget $title ) { |
||
230 | |||
231 | /** |
||
232 | * Normalizes and splits a title string. |
||
233 | * |
||
234 | * This function removes illegal characters, splits off the interwiki and |
||
235 | * namespace prefixes, sets the other forms, and canonicalizes |
||
236 | * everything. |
||
237 | * |
||
238 | * @todo this method is only exposed as a temporary measure to ease refactoring. |
||
239 | * It was copied with minimal changes from Title::secureAndSplit(). |
||
240 | * |
||
241 | * @todo This method should be split up and an appropriate interface |
||
242 | * defined for use by the Title class. |
||
243 | * |
||
244 | * @param string $text |
||
245 | * @param int $defaultNamespace |
||
246 | * |
||
247 | * @throws MalformedTitleException If $text is not a valid title string. |
||
248 | * @return array A map with the fields 'interwiki', 'fragment', 'namespace', |
||
249 | * 'user_case_dbkey', and 'dbkey'. |
||
250 | */ |
||
251 | public function splitTitleString( $text, $defaultNamespace = NS_MAIN ) { |
||
449 | |||
450 | /** |
||
451 | * Returns a simple regex that will match on characters and sequences invalid in titles. |
||
452 | * Note that this doesn't pick up many things that could be wrong with titles, but that |
||
453 | * replacing this regex with something valid will make many titles valid. |
||
454 | * Previously Title::getTitleInvalidRegex() |
||
455 | * |
||
456 | * @return string Regex string |
||
457 | * @since 1.25 |
||
458 | */ |
||
459 | public static function getTitleInvalidRegex() { |
||
478 | } |
||
479 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.