一个文件应该声明新的符号(类、函数、常量等)并且不会引起其他副作用,或者它应该执行带有副作用的逻辑,但不应该两者都做。
考虑一个 config.php 文件中的这个例子(我自己的):
/**
* Parsing the database URL.
* DATABASE_URL is in the form:
* postgres://user:password@hostname:port/database
* e.g.:
* postgres://u123:pabc@ec2.eu-west-1.compute.amazonaws.com:5432/dxyz
*/
$url = parse_url(getenv('DATABASE_URL'));
define('DB_HOST', $url['host']);
define('DB_NAME', substr($url['path'], 1)); // get rid of initial slash
define('DB_USER', $url['user']);
define('DB_PASSWORD', $url['pass']);
Run Code Online (Sandbox Code Playgroud)
如果我这样做,我实际上没有尊重建议。phpcs理所当然地会抱怨它,因为变量:
FILE: config.php
-----------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
-----------------------------------------------------------------------------------------------------------------
1 | WARNING | A file should declare new symbols (classes, functions, constants, etc.) and cause no other side …Run Code Online (Sandbox Code Playgroud)