Popular Coding Convention in PHP on Github - Shortlist
class Foo
{
// ...
}
class Foo {
// ...
}
if($baz) {
// ..
} else {
// ..
}
if($baz) {
// ..
}
else {
// ..
}
if($baz)
{
// ..
}
else
{
// ..
}
function bar($baz)
{
// ...
}
function bar($baz) {
// ...
}
<?php
opening token<?php
opening token<?php
// The line above this comment is empty
// ...
<?php
opening token<?php
// The line above this comment is used
// ...
class Foo {
function bar($baz) {
// uses two spaces for indentation
}
}
class Foo {
function bar($baz) {
// uses four spaces for indentation
}
}
class Foo {
function bar($baz) {
// uses one tab for indentation
}
}
switch($baz){
case 'bar':
// Regardless of where the `break` is
}
switch($baz){
case 'bar':
// Regardless of where the `break` is
}
switch($baz){
case 'bar':
// Regardless of where the `break` is
}
switch($baz){
// Regardless of where the `case` is
break;
}
switch($baz){
// Regardless of where the `case` is
break;
}
switch($baz){
// Regardless of where the `case` is
break;
}
echo 'foo';
// ...
echo 'foo';
// ...
echo 'foo';
// ...
if ($baz) {
// ...
}
if($baz){
// ...
}
if ( $baz ) {
// ...
}
if($baz) {
// ...
}
function bar ($baz) {
// ...
}
function bar($baz){
// ...
}
function bar( $baz ){
// ...
}
function bar($baz){
// ...
}
class fooBarBaz {
// ...
}
class FooBarBaz {
// ...
}
class FOO_BAR_BAZ {
// ...
}
class Foo_Bar_Baz {
// ...
}
class foo_bar_baz {
// ...
}
class Foo_bar_baz {
// ...
}
class Foo {
const barBaz = 0;
}
class Foo {
const BarBaz = 0;
}
class Foo {
const BAR_BAZ = 0;
}
class Foo {
const Bar_Baz = 0;
}
class Foo {
const bar_baz = 0;
}
class Foo {
public function barBaz(){
// ...
}
}
class Foo {
public function BarBaz(){
// ...
}
}
class Foo {
public function BAR_BAZ(){
// ...
}
}
class Foo {
public function Bar_Baz(){
// ...
}
}
class Foo {
public function bar_baz(){
// ...
}
}
class Foo {
protected $barBaz;
}
class Foo {
protected $BarBaz;
}
class Foo {
protected $BAR_BAZ;
}
class Foo {
protected $Bar_Baz;
}
class Foo {
protected $bar_baz;
}
define('barBaz', 0);
define('BarBaz', 0);
define('BAR_BAZ', 0);
define('Bar_Baz', 0);
define('bar_baz', 0);
function barBaz(){
// ...
}
function BarBaz(){
// ...
}
function BAR_BAZ(){
// ...
}
function Bar_Baz(){
// ...
}
function bar_baz(){
// ...
}
namespace Vendor\Package {
class Foo {
// ...
}
}
class Vendor_Package_Foo {
// ...
}
$baz = true;
$baz = false;
$baz = null;
$baz = TRUE;
$baz = FALSE;
$baz = NULL;
try {
if(){
switch(){
// ...
}
}
} catch (){
// ...
}
TRY {
IF(){
SWITCH(){
// ...
}
}
} CATCH (){
// ...
}
static
declared after visibilityclass Foo
{
public static function bar($baz)
{
// ...
}
}
static
declared before visibilityclass Foo
{
static public function bar($baz)
{
// ...
}
}
abstract
(or final
) declared after visibilityclass Foo
{
public abstract function bar($baz);
// ...
}
abstract
(or final
) declared before visibilityclass Foo
{
abstract public function bar($baz);
// ...
}
/**
* The Foo Class
*
* Takes care of all of the foos
*/
class Foo {
/**
* Bar a given Baz
*
* @param $baz
*
* @return bool
*/
function bar($baz) {
// ...
}
}
class Foo {
function bar($baz) {
// ...
}
}
?>
allowed or manditory<?php
// ...
?>
?>
not allowed<?php
// ...
#EOF
<p>
<? echo $baz ?>
</p>
<p>
<?php echo $baz ?>
</p>
<p>
<?= $baz ?>
<p>
<p>
<? echo $baz ?>
</p>
if($baz) {
// ..
} else {
// ..
}
if($baz)
// ..
else
// ..
// Break before hitting the 80 character line
//----------------------------------40-↓----------------------------------[80]-↓-----------------------------------120-↓-----------------------------------160-↓-----------------------------------200-↓-----------------------------------[∞]-→
$iterator = new RecursiveDirectoryIterator(
$folder, FilesystemIterator::CURRENT_AS_FILEINFO
| FilesystemIterator::KEY_AS_PATHNAME
| FilesystemIterator::FOLLOW_SYMLINKS | FilesystemIterator::SKIP_DOTS
| FilesystemIterator::UNIX_PATHS
);
// Break before hitting the 100 character line
//----------------------------------40-↓------------------------------------80-↓-------------[100]-↓---------------120-↓-----------------------------------160-↓-----------------------------------200-↓-----------------------------------[∞]-→
$iterator = new RecursiveDirectoryIterator(
$folder, FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_PATHNAME
| FilesystemIterator::FOLLOW_SYMLINKS | FilesystemIterator::SKIP_DOTS
| FilesystemIterator::UNIX_PATHS
);
// Break before hitting the 120 character line
//----------------------------------40-↓------------------------------------80-↓---------------------------------[120]-↓-----------------------------------160-↓-----------------------------------200-↓-----------------------------------[∞]-→
$iterator = new RecursiveDirectoryIterator(
$folder, FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_PATHNAME
| FilesystemIterator::FOLLOW_SYMLINKS | FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS
);
// I Break for no-one!
//----------------------------------40-↓------------------------------------80-↓-----------------------------------120-↓-----------------------------------160-↓-----------------------------------200-↓-----------------------------------[∞]-→
$iterator = new RecursiveDirectoryIterator($folder, FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::FOLLOW_SYMLINKS | FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS);
if($baz === 'baz'){
// ...
}
if('baz' === $baz){
// ...
}
$baz = 'baz';
$quux = 'qux';
$foozle = 'foo';
$baz = 'baz';
$quux = 'qux';
$foozle = 'foo';
# This is a comment
// This is a comment
/* This is a comment */
class Foo {
protected function bar($baz) {
// ...
}
}
class Foo {
protected function _bar($baz) {
// ...
}
}
The following list contains all of the conventions I can think of to include in the convention list. It is based on a comparison of various conventions as dictated by various more well-know projects.
Some of these may be dropped under influence of comments or lack of good examples.
<?php
opening tokenThere are several things mentioned that I don't think fall under "code style clash" but rather good/bad practices. I don't think these need to be included. Also, various things might be too tricky to measure and should be left out because of that.
//
, #
and /**/
. However,
there would have to be a check to take docblocks into account /** */
,
otherwise that would throw everyting of in favour of /**/
.error_reporting
but then integers could be used instead of constants, making the whole thing
more tiresome than seems worth the effort.$
). Could be worthwhile to find out. Not sure.EOF
For everyone's entertainment I though it might be nice to get PHP added to the Popular Coding Convention on Github site.
A request had already been made so I decided to pick up the gauntlet.
My plan of attack is as follows:
[UPDATE]:
After creating a list based on the PSR2 I began to get this feeling of uncertainty that it might be incomplete. It was also unclear from the PSR2 alone where conflicts would arise with other conventions. So rather than to just guess and hope for the best I decided to take several coding standards and map their conventions and that way I would have an actual basis in fact for my set of possible convention conflicts. I got a bit too obsessed with the detail for a while there, causing some delay. Various yaks needed shaving an I changed jobs during that period as well, but I got my act together in the end.
The best place to go to find out which coding style clashes we as a community have seemed to me to be the FIG site. Based on the PSR-1 and PSR-2 I came up with the list below.
Several weekendends later, my list sort of turned into a side project in it's own right but putting it all back together and drawing it back into the scope of this project, it looks complete.
The first people that come to mind, other than my co-hooligans are @skoop, @jaytaph, @mvriel, @rdohms, @dr4goonis and maybe @philsturgeon.
From the feedback I received it would seem nothing is missing, so all is good!
Ellipsis!
Or just have some more fun, maybe? In either case, the information here seems complete and is ready for use.