Complex classes like MessageSource_PHP 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 MessageSource_PHP, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class MessageSource_PHP extends MessageSource |
||
|
|
|||
| 38 | { |
||
| 39 | /** |
||
| 40 | * Message data filename extension. |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $dataExt = '.php'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Separator between culture name and source. |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $dataSeparator = '.'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Constructor. |
||
| 53 | * @param string the directory where the messages are stored. |
||
| 54 | * @see MessageSource::factory(); |
||
| 55 | */ |
||
| 56 | function __construct($source) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Load the messages from a PHP file. |
||
| 63 | * @param string PHP file. |
||
| 64 | * @return array of messages. |
||
| 65 | */ |
||
| 66 | protected function &loadData($filename) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Get the last modified unix-time for this particular catalogue+variant. |
||
| 90 | * Just use the file modified time. |
||
| 91 | * @param string catalogue+variant |
||
| 92 | * @return int last modified in unix-time format. |
||
| 93 | */ |
||
| 94 | protected function getLastModified($source) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get the PHP file for a specific message catalogue and cultural |
||
| 101 | * variant. |
||
| 102 | * @param string message catalogue |
||
| 103 | * @return string full path to the PHP file. |
||
| 104 | */ |
||
| 105 | protected function getSource($variant) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Determin if the PHP file source is valid. |
||
| 112 | * @param string PHP file |
||
| 113 | * @return boolean true if valid, false otherwise. |
||
| 114 | */ |
||
| 115 | protected function isValidSource($source) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Get all the variants of a particular catalogue. |
||
| 122 | * @param string catalogue name |
||
| 123 | * @return array list of all variants for this catalogue. |
||
| 124 | */ |
||
| 125 | protected function getCatalogueList($catalogue) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Traverse through the directory structure to find the catalogues. |
||
| 147 | * This should only be called by getCatalogueList() |
||
| 148 | * @param string a particular catalogue. |
||
| 149 | * @return array a list of catalogues. |
||
| 150 | * @see getCatalogueList() |
||
| 151 | */ |
||
| 152 | private function getCatalogueByDir($catalogue) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Returns a list of catalogue and its culture ID. |
||
| 172 | * E.g. array('messages','en_AU') |
||
| 173 | * @return array list of catalogues |
||
| 174 | * @see getCatalogues() |
||
| 175 | */ |
||
| 176 | public function catalogues() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Returns a list of catalogue and its culture ID. This takes care |
||
| 183 | * of directory structures. |
||
| 184 | * E.g. array('messages','en_AU') |
||
| 185 | * @return array list of catalogues |
||
| 186 | */ |
||
| 187 | protected function getCatalogues($dir=null,$variant=null) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Get the variant for a catalogue depending on the current culture. |
||
| 227 | * @param string catalogue |
||
| 228 | * @return string the variant. |
||
| 229 | * @see save() |
||
| 230 | * @see update() |
||
| 231 | * @see delete() |
||
| 232 | */ |
||
| 233 | private function getVariants($catalogue='messages') |
||
| 248 | |||
| 249 | protected function internalSaveFile($php, $filename) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Save the list of untranslated blocks to the translation source. |
||
| 265 | * If the translation was not found, you should add those |
||
| 266 | * strings to the translation source via the <b>append()</b> method. |
||
| 267 | * @param string the catalogue to add to |
||
| 268 | * @return boolean true if saved successfuly, false otherwise. |
||
| 269 | */ |
||
| 270 | public function save($catalogue='messages') |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Update the translation. |
||
| 306 | * @param string the source string. |
||
| 307 | * @param string the new translation string. |
||
| 308 | * @param string comments |
||
| 309 | * @param string the catalogue to save to. |
||
| 310 | * @return boolean true if translation was updated, false otherwise. |
||
| 311 | */ |
||
| 312 | public function update($text, $target, $comments, $catalogue='messages') |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Delete a particular message from the specified catalogue. |
||
| 347 | * @param string the source message to delete. |
||
| 348 | * @param string the catalogue to delete from. |
||
| 349 | * @return boolean true if deleted, false otherwise. |
||
| 350 | */ |
||
| 351 | public function delete($message, $catalogue='messages') |
||
| 379 | |||
| 380 | protected function createMessageTemplate($catalogue) |
||
| 405 | |||
| 406 | protected function getTemplate($catalogue) |
||
| 424 | } |
||
| 425 |
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider.