Complex classes like Lang 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 Lang, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Lang |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * 配置参数 |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | protected $config = [ |
||
| 26 | // 默认语言 |
||
| 27 | 'default_lang' => 'zh-cn', |
||
| 28 | // 允许的语言列表 |
||
| 29 | 'allow_lang_list' => [], |
||
| 30 | // 是否使用Cookie记录 |
||
| 31 | 'use_cookie' => true, |
||
| 32 | // 扩展语言包 |
||
| 33 | 'extend_list' => [], |
||
| 34 | // 多语言cookie变量 |
||
| 35 | 'cookie_var' => 'think_lang', |
||
| 36 | // 多语言自动侦测变量名 |
||
| 37 | 'detect_var' => 'lang', |
||
| 38 | // Accept-Language转义为对应语言包名称 |
||
| 39 | 'accept_language' => [ |
||
| 40 | 'zh-hans-cn' => 'zh-cn', |
||
| 41 | ], |
||
| 42 | // 是否支持语言分组 |
||
| 43 | 'allow_group' => false, |
||
| 44 | ]; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * 多语言信息 |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | private $lang = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * 当前语言 |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $range = 'zh-cn'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * 构造方法 |
||
| 60 | * @access public |
||
| 61 | * @param array $config |
||
| 62 | */ |
||
| 63 | 3 | public function __construct(array $config = []) |
|
| 68 | |||
| 69 | 3 | public static function __make(Config $config) |
|
| 73 | |||
| 74 | /** |
||
| 75 | * 设置当前语言 |
||
| 76 | * @access public |
||
| 77 | * @param string $lang 语言 |
||
| 78 | * @return void |
||
| 79 | */ |
||
| 80 | public function setLangSet(string $lang): void |
||
| 84 | |||
| 85 | /** |
||
| 86 | * 获取当前语言 |
||
| 87 | * @access public |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | public function getLangSet(): string |
||
| 94 | |||
| 95 | /** |
||
| 96 | * 获取默认语言 |
||
| 97 | * @access public |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | 3 | public function defaultLangSet() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * 加载语言定义(不区分大小写) |
||
| 107 | * @access public |
||
| 108 | * @param string|array $file 语言文件 |
||
| 109 | * @param string $range 语言作用域 |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | 3 | public function load($file, $range = ''): array |
|
| 134 | |||
| 135 | /** |
||
| 136 | * 解析语言文件 |
||
| 137 | * @access protected |
||
| 138 | * @param string $file 语言文件名 |
||
| 139 | * @return array |
||
| 140 | */ |
||
| 141 | 3 | protected function parse(string $file): array |
|
| 159 | |||
| 160 | /** |
||
| 161 | * 判断是否存在语言定义(不区分大小写) |
||
| 162 | * @access public |
||
| 163 | * @param string|null $name 语言变量 |
||
| 164 | * @param string $range 语言作用域 |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | public function has(string $name, string $range = ''): bool |
||
| 178 | |||
| 179 | /** |
||
| 180 | * 获取语言定义(不区分大小写) |
||
| 181 | * @access public |
||
| 182 | * @param string|null $name 语言变量 |
||
| 183 | * @param array $vars 变量替换 |
||
| 184 | * @param string $range 语言作用域 |
||
| 185 | * @return mixed |
||
| 186 | */ |
||
| 187 | public function get(string $name = null, array $vars = [], string $range = '') |
||
| 227 | |||
| 228 | /** |
||
| 229 | * 自动侦测设置获取语言选择 |
||
| 230 | * @access public |
||
| 231 | * @param Request $request |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | public function detect(Request $request): string |
||
| 263 | |||
| 264 | /** |
||
| 265 | * 保存当前语言到Cookie |
||
| 266 | * @access public |
||
| 267 | * @param Cookie $cookie Cookie对象 |
||
| 268 | * @return void |
||
| 269 | */ |
||
| 270 | public function saveToCookie(Cookie $cookie) |
||
| 276 | |||
| 277 | } |
||
| 278 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.