[TYPO3-core] CGL proposal: Include PSR-1 Side Effects-Rule
Andy Grunwald [wmdb]
andy_grunwald at arcor.de
Sat Oct 20 23:39:04 CEST 2012
Hey @all,
currently, we (the PHP_CodeSniffer-Team [1]) evaluating [2] the PSR-1
standard of the PHP Framework Interoperability Group (PHP-FIG) [3] for
TYPO3v4.
This standard creates a recommendation about PHP files which contains
Classes / Interfaces / Functions / Constants AND executable code like
(ini_set, ...). This recommendation is called "Side Effects".
Here is the rule (for lazy people who dont want to click [3] :)):
==================================
2.3. Side Effects
A file SHOULD declare new symbols (classes, functions, constants, etc.)
and cause no other side effects, or it SHOULD execute logic with side
effects, but SHOULD NOT do both.
The phrase "side effects" means execution of logic not directly related
to declaring classes, functions, constants, etc., merely from including
the file.
"Side effects" include but are not limited to: generating output,
explicit use of require or include, connecting to external services,
modifying ini settings, emitting errors or exceptions, modifying global
or static variables, reading from or writing to a file, and so on.
The following is an example of a file with both declarations and side
effects; i.e, an example of what to avoid:
<?php
// side effect: change ini settings
ini_set('error_reporting', E_ALL);
// side effect: loads a file
include "file.php";
// side effect: generates output
echo "<html>\n";
// declaration
function foo()
{
// function body
}
The following example is of a file that contains declarations without
side effects; i.e., an example of what to emulate:
<?php
// declaration
function foo()
{
// function body
}
// conditional declaration is *not* a side effect
if (! function_exists('bar')) {
function bar()
{
// function body
}
}
==================================
Now my proposal:
Lets include this rule!
With this rule, most of our files are more reusable without side effects
(oh you got the reason for the name :)).
Further more in my opinion isn`t it wrong to adapt a standard.
What do you think about this rule?
Is it useful? I want to hear your voice!
Thanks.
All the best,
Andy
[1] http://forge.typo3.org/projects/show/team-php_codesniffer
[2] http://forge.typo3.org/issues/42139
[3]
https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md
More information about the TYPO3-team-core
mailing list