[TYPO3-core] Decisions and Changes to Coding Guidelines (CGL)

Oliver Hader oliver.hader at typo3.org
Thu Jan 30 13:37:33 CET 2014


Hello dear TYPO3 folks,

as discussed [1, 2, 3] in the previous weeks and months, we decided on
the following changes to the current coding guidelines. All are relevant
for PHP only.

1) *Type casting:* Use short versions of the PHP-used type casting

a) For casting to different types, the PHP-internal casting
functionality must be used:

see
http://us.php.net/manual/en/language.types.type-juggling.php#language.types.typecasting

This relates to the following changes:

*Examples:*
- Use (int) instead of intval() or (integer)
- Use (bool) instead of boolval() or (boolean)
- Use (string) instead of strval()

b) For doc comments, the short versions must be used as well:

*Examples:*
- @var int $mynumber
- @var bool $mycheck
- @var string $mycheck


2) *Strict comparisons* (===) should always be used over loose
comparisons (==)


3) *Compare for a string* within a string

a) Use strpos/stripos instead of strstr/stristr

To compare if a string is within another string, use
  if (strpos('mylongtext', 'long') !== FALSE)
instead of
  if (strstr('mylongtext', 'long') !== '')

b) When checking if one part starts in another
"GeneralUtility::isFirstPartOfStr()" shall be used.


4) *Check for file existence*

When not using the FAL API, but using file-functions from PHP directly,
use file_exists() over is_file(). Only when to check, if it is really a
file and not a link or directory, use is_file(). This is important for
consistency and performance reasons.


5) *Conditions*

a) All ternary conditions should have parenthesis for better readability
in the context.

b) Sort ternary functionality should be used when the condition equals
the result.

*Examples:*

// ternary condition:
$finalValue = ($conf['myvar'] ? $conf['myvar'] : 'defaultvalue');

// short ternary condition:
$finalValue = ($conf['myvar'] ?: 'defaultvalue');

Spaces between ?: for short ternaries are not allowed.


The changes are valid effectively today, the currently published version
of the coding guidelines will be adapted and re-published accordingly.
CGL changes are allowed to be merged to the current master branch of the
TYPO3 Core only (no mass changes in released branches)!

There are some more CGL topics that will be discussed and decided on in
the future. For the time being, the current decision helps us to avoid
superfluous discussions during the review process and enables us to
actually focus on development.

Thanks for all of your feedback in the past and understanding for this
final decision!


All the best,
Olly


[1] https://notes.typo3.org/p/CGL-decisions
[2] http://doodle.com/b7ycp8y8g9zfxgfv
[3] http://doodle.com/xm7wysupewg5gba9
-- 
Oliver Hader
TYPO3 CMS Core Team Leader

TYPO3 .... inspiring people to share!
Get involved: http://typo3.org


More information about the TYPO3-team-core mailing list