SlideShare une entreprise Scribd logo
1  sur  42
Télécharger pour lire hors ligne
PHP Coding Standards
For Developers
Presented On 13-Feb-2014
By,
Sangeeta Arora – Technical Lead | CIPL
sangeeta.arora@classicinformatics.com

1

This is private and confidential material
Agenda
Introduce PSR





What is PSR?



Different types of PSR

Introduce DocBlocks





What is DocBlock?



Different DocBlock Tags

Introduce PHP_CodeSniffer





2

What is PHP_CodeSniffer?

This is private and confidential material
PSR?
PHP Standards Recommendation.



PSR stands for



Coding standards are guidelines for code style and documentation.
Purpose of coding standards is to make it so that source code looks as if it’s
written by a single developer as it sets a level of expectations for contributing
developers.
Five Types of PSR
 PSR – 0 – Autoloading Standard
 PSR – 1 – Basic Coding Standard
 PSR – 2 – Coding Style Guide
 PSR – 3 – Logger Interface
 PSR – 4 – Improved Autoloading
We will discuss PSR – 0, PSR – 1 and PSR – 2 in the further slides.
We will discuss PSR – 3 and PSR – 4 in future presentations.








3

This is private and confidential material
PSR – 0 – Autoloading Standard
A standard recommendation that describes
“the mandatory requirements that must be
adhered to for autoloader interoperability.

4

This is private and confidential material
PSR – 0 – Autoloading Standard


A fully-qualified namespace and class must have the following structure <Vendor
Name>(<Namespace>)*<Class Name>.



Each namespace must have a top-level namespace ("Vendor Name").



Each namespace can have as many sub-namespaces as it wishes.



Each namespace separator is converted to a DIRECTORY_SEPARATOR when loading from
the file system.



Each _ character in the CLASS NAME is converted to a DIRECTORY_SEPARATOR.
The _ character has no special meaning in the namespace.



The fully-qualified namespace and class is suffixed with .php when loading from the file
system.



Alphabetic characters in vendor names, namespaces, and class names may be of any
combination of lower case and upper case.

5

This is private and confidential material
PSR – 0 – Examples


DoctrineCommonIsolatedClassLoader => /path/to/project/lib/vendor/Doctrine/Common/
IsolatedClassLoader.php



SymfonyCoreRequest => /path/to/project/lib/vendor/Symfony/Core/Request.php.



ZendAcl => /path/to/project/lib/vendor/Zend/Acl.php.



ZendMailMessage => /path/to/project/lib/vendor/Zend/Mail/Message.php.

6

This is private and confidential material
PSR – 1 – Basic Coding Standard
It focuses on a Basic Coding Standard.

7

This is private and confidential material
PSR – 1 – Basic Coding Standard


Files MUST use only <?php and <?= tags.



Files MUST use only UTF-8 (without BOM) character encoding for PHP code.



Files SHOULD either declare symbols (classes, functions, constants, etc.) or cause sideeffects (e.g. generate output, change .ini settings, etc.) but SHOULD NOT do both.



Namespaces and classes MUST follow PSR-0.



Class names MUST be declared in StudlyCaps.



Class constants MUST be declared in all upper case with underscore separators, if required.
E.g. const VERSION; const DATE_APPROVED;



Class method names MUST be declared in camelCase.
8

This is private and confidential material
PSR – 2 – Coding Style Guide
Its purpose is to have a single style
guide for PHP code that results in
uniformly formatted shared code.

9

This is private and confidential material
PSR – 2 – Coding Style Guide


Basic Coding Standard




Code MUST follow PSR-1.

Files



All PHP files MUST end with a single blank line.





All PHP files MUST use the Unix LF (linefeed) line ending.
The closing ?> tag MUST be omitted from files containing only PHP.

Lines


There MUST NOT be a hard limit on line length.



The soft limit MUST be 120 characters; lines SHOULD be 80 characters or less.



There MUST NOT be trailing whitespace at the end of non-blank lines.



Blank lines MAY be added to improve readability and to indicate related blocks of code.



There MUST NOT be more than one statement per line.

10

This is private and confidential material
PSR – 2 – Coding Style Guide …contd.


Indenting




Code MUST use 4 spaces for indenting, and MUST NOT use tabs for indenting.

Keywords and True/False/Null





PHP keywords MUST be in lower case.
The PHP constants true, false, and null MUST be in lower case.

Namespace and Use Declarations


There MUST be one blank line after the namespace declaration.



All use declarations MUST go after the namespace declaration.



There MUST be one use keyword per declaration..



There MUST be one blank line after the use block.

11

This is private and confidential material
PSR – 2 – Coding Style Guide …contd.


Extends and Implements
 The extends and implements keywords MUST be declared on the same line as the class
name. e.g.



Lists of implements MAY be split across multiple lines, where each subsequent line is
indented once. When doing so, the first item in the list MUST be on the next line, and
there MUST be only one interface per line.

Opening braces for classes MUST go on the next line, and closing braces MUST go on the
next line after the body.
Properties
 Visibility MUST be declared on all properties.




12

This is private and confidential material
PSR – 2 – Coding Style Guide …contd.


Properties



The var keyword MUST NOT be used to declare a property.





There MUST NOT be more than one property declared per statement.

Property names SHOULD NOT be prefixed with a single underscore to indicate protected
or private visibility. e.g.

Methods


Visibility MUST be declared on all methods.



Method names SHOULD NOT be prefixed with a single underscore to indicate protected
or private visibility.



Method names MUST NOT be declared with a space after the method name.



The opening brace MUST go on its own line, and the closing brace MUST go on the next
line following the body.

13

This is private and confidential material
PSR – 2 – Coding Style Guide …contd.


Methods




There MUST NOT be a space after the opening parenthesis, and there MUST NOT be a
space before the closing parenthesis. e.g.

Method Arguments


There MUST NOT be a space before each comma.



There MUST be one space after each comma.



Method arguments with default values MUST go at the end of the argument list.

14

This is private and confidential material
PSR – 2 – Coding Style Guide …contd.


Method Arguments

Example of Arguments in
the same line

Example of Arguments not
in the same line, when have
a long list of arguments.

15

This is private and confidential material
PSR – 2 – Coding Style Guide …contd.


Qualifiers (abstract, final, and static)
 When present, the abstract and final declarations MUST precede the visibility
declaration.


16

When present, the static declaration MUST come after the visibility declaration.

This is private and confidential material
PSR – 2 – Coding Style Guide …contd.


Method and Function Calls


There MUST NOT be a space between the method or function name and the opening
parenthesis.



There MUST NOT be a space after the opening parenthesis.



There MUST NOT be a space before the closing parenthesis.



In the argument list, there MUST NOT be a space before each comma, and there MUST
be one space after each comma.

Example of Arguments in
the same line
17

Example of Arguments not in the same
line, when have a long list of arguments.
This is private and confidential material
PSR – 2 – Coding Style Guide …contd.


Control Structures


There MUST be one space after the control structure keyword.



There MUST NOT be a space after the opening parenthesis.



There MUST NOT be a space before the closing parenthesis.



There MUST be one space between the closing parenthesis and the opening brace.



The structure body MUST be indented once.



The closing brace MUST be on the next line after the body.

18

This is private and confidential material
PSR – 2 – Coding Style Guide …contd.


if-elseif-else


The keyword elseif SHOULD be used instead of else if.



else and elseif are on the same line as the closing brace from the earlier body.



Note the placement of parentheses, spaces, and braces.

19

This is private and confidential material
PSR – 2 – Coding Style Guide …contd.


switch and case


The case statement MUST be indented once from switch.



The break keyword (or other terminating keyword) MUST be indented at the same level
as the case body.



There MUST be a comment such as // no break when fall-through is intentional in a nonempty case body.



Note the placement of parentheses, spaces, and braces.

20

This is private and confidential material
PSR – 2 – Coding Style Guide …contd.


while and do-while


Note the placement of parentheses, spaces, and braces for while statement.



Note the placement of parentheses, spaces, and braces for do-while statement.

21

This is private and confidential material
PSR – 2 – Coding Style Guide …contd.


for and foreach


Note the placement of parentheses, spaces, and braces for for statement.



Note the placement of parentheses, spaces, and braces for foreach statement.

22

This is private and confidential material
PSR – 2 – Coding Style Guide …contd.


try-catch


23

Note the placement of parentheses, spaces, and braces for try-catch statement.

This is private and confidential material
PSR – 2 – Coding Style Guide …contd.


In-Code Documentation


All PHP files should include a DocBlock header.



All classes should include a DocBlock for the class definition.



All class constants, properties and methods should include a DocBlock to explain their
purpose.



All functions should include a DocBlock to explain their purpose.

24

This is private and confidential material
PSR – 3 – Logger Interface
PSR-3 describes a shared logging
interface which improves reusability.

25

This is private and confidential material
PSR – 4 – Improved Autoloading
The main goal of PSR-4 is to remove the
remnants of PSR-0 and the pre-5.3 days
completely and allow for a more concise
folder structure.

26

This is private and confidential material
DocBlocks
DocBlock is a multi-line C-style comment
used to document a block of code.

27

This is private and confidential material
DocBlocks


It is a multi-line C-style comment used to document a block of code.



"DocComment" is a special type of comment which MUST


It begins with the character sequence /** followed by a whitespace character



It has an asterisk at the start of each line.
end with */
have zero or more lines in between






DocBlocks consist of three sections:



short description
long description
tags



Short description is a concise description terminated by either a new-line or a period.



Long description is where the bulk of the documentation goes; it can be multi-line and as
long you wish.
28

This is private and confidential material
DocBlocks …contd.


Tags


The tags are used to specify additional information, such as the expected parameters and their type.



Each tag starts on a new line, followed by an at-sign (@) and a tag-name followed by white-space and
meta-data (including a description).

29

This is private and confidential material
DocBlocks …contd.


Different Types of Tags
























30

@api
@author
@copyright
@deprecated
@example
@global
@internal
@license
@method
@package
@param
@property
@return
@see
@since
@struct
@throws
@todo
@type
@uses
@version
This is private and confidential material
DocBlocks …contd.


@api – It is used to declare "Structural Elements" as being suitable for consumption by
third parties.



@author – The @author tag is used to document the author of any "Structural Element".



@copyright – The @copyright tag is used to document the copyright information of any
"Structural Element".

31

This is private and confidential material
DocBlocks …contd.


@deprecated – The @deprecated tag is used to indicate which 'Structural elements' are
deprecated and are to be removed in a future version.



@example – The @example tag is used to link to an external source code file which contains
an example of use for the current "Structural element".

32

This is private and confidential material
DocBlocks …contd.


@global – The @global tag is used to denote a global variable or its usage.



@license – The @license tag is used to indicate which license is applicable for the associated
'Structural Elements'.



@method – The @method allows a class to know which 'magic' methods are callable.

33

This is private and confidential material
DocBlocks …contd.


@package – The @package tag is used to categorize "Structural Elements" into logical
subdivisions.



@param – The @param tag is used to document a single parameter of a function or
method.



@property – The @property tag is used in the situation where a class contains the __get()
and __set() magic methods and allows for specific names.

34

This is private and confidential material
DocBlocks …contd.


@return – The @return tag is used to document the return value of functions or methods.



@see – The @see tag can be used to define a reference to other "Structural Elements" or to
a URI.

35

This is private and confidential material
DocBlocks …contd.


@since – The @since tag is used to denote when an element was introduced or modified,
using some description of "versioning" to that element.



The @since tag SHOULD NOT be used to show the current version of an element, the
@version tag MAY be used for that purpose.



@todo – The @todo tag is used to indicate whether any development activities should still
be executed on associated "Structural Elements".

36

This is private and confidential material
DocBlocks …contd.


@throws – The @throws tag is used to indicate whether "Structural Elements" throw a
specific type of exception.



@version – The @version tag is used to denote some description of "versioning" to an
element.

37

This is private and confidential material
DocBlocks …contd.


@type – The @type tag defines which type of data is represented by a value of a Constant,
Property or Variable.



@uses – The @uses tag describes whether any part of the associated "Structural Element"
uses, or consumes, another "Structural Element" or a file that is situated in the current
project.

38

This is private and confidential material
PHP_CodeSniffer

39

This is private and confidential material
What is PHP_CodeSniffer?


PHP_CodeSniffer (PHPCS) is a PHP5 script that tokenizes PHP, JavaScript and CSS files to
detect violations of a defined coding standard.



It is an essential development tool that ensures your code remains clean and consistent.



It can also help prevent some common semantic errors made by developers.



It allows you to not only ensure that your code follows the PSR standards, but it also uses
PHP’s linter to check for syntax errors.



Multiple coding standards can be used within PHP_CodeSniffer so that the one installation
can be used across multiple projects.



The default coding standard used by PHP_CodeSniffer is the PEAR coding standard.

40

This is private and confidential material
Q&A

41

This is private and confidential material
Thank You

42

This is private and confidential material

Contenu connexe

Tendances

JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript TutorialBui Kiet
 
python ppt | Python Course In Ghaziabad | Scode Network Institute
python ppt | Python Course In Ghaziabad | Scode Network Institutepython ppt | Python Course In Ghaziabad | Scode Network Institute
python ppt | Python Course In Ghaziabad | Scode Network InstituteScode Network Institute
 
JavaScript - Chapter 5 - Operators
 JavaScript - Chapter 5 - Operators JavaScript - Chapter 5 - Operators
JavaScript - Chapter 5 - OperatorsWebStackAcademy
 
Oops abap fundamental
Oops abap fundamentalOops abap fundamental
Oops abap fundamentalbiswajit2015
 
Python Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaEdureka!
 
Oo abap-sap-1206973306636228-5
Oo abap-sap-1206973306636228-5Oo abap-sap-1206973306636228-5
Oo abap-sap-1206973306636228-5prakash185645
 
ASP.NET Core MVC + Web API with Overview
ASP.NET Core MVC + Web API with OverviewASP.NET Core MVC + Web API with Overview
ASP.NET Core MVC + Web API with OverviewShahed Chowdhuri
 
Ooabap notes with_programs
Ooabap notes with_programsOoabap notes with_programs
Ooabap notes with_programsKranthi Kumar
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions WebStackAcademy
 
Introduction to Sightly and Sling Models
Introduction to Sightly and Sling ModelsIntroduction to Sightly and Sling Models
Introduction to Sightly and Sling ModelsStefano Celentano
 
Python web frameworks
Python web frameworksPython web frameworks
Python web frameworksNEWLUG
 
Sap Abap Reports
Sap Abap ReportsSap Abap Reports
Sap Abap Reportsvbpc
 
Python Course | Python Programming | Python Tutorial | Python Training | Edureka
Python Course | Python Programming | Python Tutorial | Python Training | EdurekaPython Course | Python Programming | Python Tutorial | Python Training | Edureka
Python Course | Python Programming | Python Tutorial | Python Training | EdurekaEdureka!
 

Tendances (20)

Basics of python
Basics of pythonBasics of python
Basics of python
 
JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript Tutorial
 
python ppt | Python Course In Ghaziabad | Scode Network Institute
python ppt | Python Course In Ghaziabad | Scode Network Institutepython ppt | Python Course In Ghaziabad | Scode Network Institute
python ppt | Python Course In Ghaziabad | Scode Network Institute
 
JavaScript - Chapter 5 - Operators
 JavaScript - Chapter 5 - Operators JavaScript - Chapter 5 - Operators
JavaScript - Chapter 5 - Operators
 
Oops abap fundamental
Oops abap fundamentalOops abap fundamental
Oops abap fundamental
 
Python Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | Edureka
 
Oo abap-sap-1206973306636228-5
Oo abap-sap-1206973306636228-5Oo abap-sap-1206973306636228-5
Oo abap-sap-1206973306636228-5
 
ASP.NET Core MVC + Web API with Overview
ASP.NET Core MVC + Web API with OverviewASP.NET Core MVC + Web API with Overview
ASP.NET Core MVC + Web API with Overview
 
Ooabap notes with_programs
Ooabap notes with_programsOoabap notes with_programs
Ooabap notes with_programs
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 
Alv theory
Alv theoryAlv theory
Alv theory
 
Step by step exercise for bw 365
Step by step exercise for bw 365Step by step exercise for bw 365
Step by step exercise for bw 365
 
Abap 7.40
Abap 7.40Abap 7.40
Abap 7.40
 
Introduction to Sightly and Sling Models
Introduction to Sightly and Sling ModelsIntroduction to Sightly and Sling Models
Introduction to Sightly and Sling Models
 
Python web frameworks
Python web frameworksPython web frameworks
Python web frameworks
 
Php ppt
Php pptPhp ppt
Php ppt
 
Abap Objects for BW
Abap Objects for BWAbap Objects for BW
Abap Objects for BW
 
Sap Abap Reports
Sap Abap ReportsSap Abap Reports
Sap Abap Reports
 
Dialog programming ABAP
Dialog programming ABAPDialog programming ABAP
Dialog programming ABAP
 
Python Course | Python Programming | Python Tutorial | Python Training | Edureka
Python Course | Python Programming | Python Tutorial | Python Training | EdurekaPython Course | Python Programming | Python Tutorial | Python Training | Edureka
Python Course | Python Programming | Python Tutorial | Python Training | Edureka
 

En vedette

Coding standards PSR-1 & PSR-2
Coding standards PSR-1 & PSR-2Coding standards PSR-1 & PSR-2
Coding standards PSR-1 & PSR-2Aram Baghdasaryan
 
Coding Standard And Code Review
Coding Standard And Code ReviewCoding Standard And Code Review
Coding Standard And Code ReviewMilan Vukoje
 
How else can you write the code in PHP?
How else can you write the code in PHP?How else can you write the code in PHP?
How else can you write the code in PHP?Maksym Hopei
 
Let's PHP in a better way! - Coding Recommendations.
Let's PHP in a better way! - Coding Recommendations.Let's PHP in a better way! - Coding Recommendations.
Let's PHP in a better way! - Coding Recommendations.Leekas Shep
 
Back to basics - PHP_Codesniffer
Back to basics - PHP_CodesnifferBack to basics - PHP_Codesniffer
Back to basics - PHP_CodesnifferSebastian Marek
 
Mari Memahami PSR (PHP Standards Recommendation)
Mari Memahami PSR (PHP Standards Recommendation)Mari Memahami PSR (PHP Standards Recommendation)
Mari Memahami PSR (PHP Standards Recommendation)Mizan Riqzia
 
Web UI performance tuning
Web UI performance tuningWeb UI performance tuning
Web UI performance tuningAndy Pemberton
 
Php Code Audits (PHP UK 2010)
Php Code Audits (PHP UK 2010)Php Code Audits (PHP UK 2010)
Php Code Audits (PHP UK 2010)Damien Seguy
 
PHP Static Code Review
PHP Static Code ReviewPHP Static Code Review
PHP Static Code ReviewDamien Seguy
 
Functions - complex first class citizen
Functions - complex first class citizenFunctions - complex first class citizen
Functions - complex first class citizenVytautas Butkus
 
Coding Best practices (PHP)
Coding Best practices (PHP)Coding Best practices (PHP)
Coding Best practices (PHP)Christian Baune
 
Modular & Event driven UI Architecture
Modular & Event driven UI ArchitectureModular & Event driven UI Architecture
Modular & Event driven UI ArchitectureVytautas Butkus
 
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...Rouven Weßling
 
JavaScript and UI Architecture Best Practices
JavaScript and UI Architecture Best PracticesJavaScript and UI Architecture Best Practices
JavaScript and UI Architecture Best PracticesSiarhei Barysiuk
 
Modern Static Code Analysis in PHP
Modern Static Code Analysis in PHPModern Static Code Analysis in PHP
Modern Static Code Analysis in PHPVladimir Reznichenko
 
Refactoring Legacy Code
Refactoring Legacy CodeRefactoring Legacy Code
Refactoring Legacy CodeAdam Culp
 
Component Based UI Architecture - Alex Moldovan
Component Based UI Architecture - Alex MoldovanComponent Based UI Architecture - Alex Moldovan
Component Based UI Architecture - Alex MoldovanITCamp
 
Modern UI Architecture_ Trends and Technologies in Web Development
Modern UI Architecture_ Trends and Technologies in Web DevelopmentModern UI Architecture_ Trends and Technologies in Web Development
Modern UI Architecture_ Trends and Technologies in Web DevelopmentSuresh Patidar
 
UI Architecture & Web Performance
UI Architecture & Web PerformanceUI Architecture & Web Performance
UI Architecture & Web PerformanceKyle Simpson
 

En vedette (20)

Coding standards PSR-1 & PSR-2
Coding standards PSR-1 & PSR-2Coding standards PSR-1 & PSR-2
Coding standards PSR-1 & PSR-2
 
Coding Standard And Code Review
Coding Standard And Code ReviewCoding Standard And Code Review
Coding Standard And Code Review
 
How else can you write the code in PHP?
How else can you write the code in PHP?How else can you write the code in PHP?
How else can you write the code in PHP?
 
Let's PHP in a better way! - Coding Recommendations.
Let's PHP in a better way! - Coding Recommendations.Let's PHP in a better way! - Coding Recommendations.
Let's PHP in a better way! - Coding Recommendations.
 
Back to basics - PHP_Codesniffer
Back to basics - PHP_CodesnifferBack to basics - PHP_Codesniffer
Back to basics - PHP_Codesniffer
 
Mari Memahami PSR (PHP Standards Recommendation)
Mari Memahami PSR (PHP Standards Recommendation)Mari Memahami PSR (PHP Standards Recommendation)
Mari Memahami PSR (PHP Standards Recommendation)
 
Web UI performance tuning
Web UI performance tuningWeb UI performance tuning
Web UI performance tuning
 
Coding standards php
Coding standards phpCoding standards php
Coding standards php
 
Php Code Audits (PHP UK 2010)
Php Code Audits (PHP UK 2010)Php Code Audits (PHP UK 2010)
Php Code Audits (PHP UK 2010)
 
PHP Static Code Review
PHP Static Code ReviewPHP Static Code Review
PHP Static Code Review
 
Functions - complex first class citizen
Functions - complex first class citizenFunctions - complex first class citizen
Functions - complex first class citizen
 
Coding Best practices (PHP)
Coding Best practices (PHP)Coding Best practices (PHP)
Coding Best practices (PHP)
 
Modular & Event driven UI Architecture
Modular & Event driven UI ArchitectureModular & Event driven UI Architecture
Modular & Event driven UI Architecture
 
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...
 
JavaScript and UI Architecture Best Practices
JavaScript and UI Architecture Best PracticesJavaScript and UI Architecture Best Practices
JavaScript and UI Architecture Best Practices
 
Modern Static Code Analysis in PHP
Modern Static Code Analysis in PHPModern Static Code Analysis in PHP
Modern Static Code Analysis in PHP
 
Refactoring Legacy Code
Refactoring Legacy CodeRefactoring Legacy Code
Refactoring Legacy Code
 
Component Based UI Architecture - Alex Moldovan
Component Based UI Architecture - Alex MoldovanComponent Based UI Architecture - Alex Moldovan
Component Based UI Architecture - Alex Moldovan
 
Modern UI Architecture_ Trends and Technologies in Web Development
Modern UI Architecture_ Trends and Technologies in Web DevelopmentModern UI Architecture_ Trends and Technologies in Web Development
Modern UI Architecture_ Trends and Technologies in Web Development
 
UI Architecture & Web Performance
UI Architecture & Web PerformanceUI Architecture & Web Performance
UI Architecture & Web Performance
 

Similaire à PHP CODING STANDARDS

Psr - php standards recommendations
Psr - php standards recommendationsPsr - php standards recommendations
Psr - php standards recommendationsHà Anh Sơn
 
Php psr standard 2014 01-22
Php psr standard 2014 01-22Php psr standard 2014 01-22
Php psr standard 2014 01-22Võ Duy Tuấn
 
2021.laravelconf.tw.slides2
2021.laravelconf.tw.slides22021.laravelconf.tw.slides2
2021.laravelconf.tw.slides2LiviaLiaoFontech
 
PSR: Standards in PHP by Alex Simanovich
PSR: Standards in PHP by Alex SimanovichPSR: Standards in PHP by Alex Simanovich
PSR: Standards in PHP by Alex SimanovichMinsk PHP User Group
 
Psr 2 coding style guide - Tidepool Labs
Psr 2   coding style guide - Tidepool LabsPsr 2   coding style guide - Tidepool Labs
Psr 2 coding style guide - Tidepool LabsHarutyun Abgaryan
 
Presentation
PresentationPresentation
Presentationbugway
 
PHP Standards Recommendations - PHP-FIG
PHP Standards Recommendations - PHP-FIGPHP Standards Recommendations - PHP-FIG
PHP Standards Recommendations - PHP-FIGAkshay Khale
 
Coding standards for java
Coding standards for javaCoding standards for java
Coding standards for javamaheshm1206
 
Aura for PHP at Fossmeet 2014
Aura for PHP at Fossmeet 2014Aura for PHP at Fossmeet 2014
Aura for PHP at Fossmeet 2014Hari K T
 
In-Depth Guide On WordPress Coding Standards For PHP & HTML
In-Depth Guide On WordPress Coding Standards For PHP & HTMLIn-Depth Guide On WordPress Coding Standards For PHP & HTML
In-Depth Guide On WordPress Coding Standards For PHP & HTMLeSparkBiz
 
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)Antonio Peric-Mazar
 
Java platform
Java platformJava platform
Java platformVisithan
 
Overview of php
Overview of phpOverview of php
Overview of phpTabeer12
 
PHP Course (Basic to Advance)
PHP Course (Basic to Advance)PHP Course (Basic to Advance)
PHP Course (Basic to Advance)Coder Tech
 
Objective-C to Swift - Swift Cloud Workshop 3
Objective-C to Swift - Swift Cloud Workshop 3Objective-C to Swift - Swift Cloud Workshop 3
Objective-C to Swift - Swift Cloud Workshop 3Randy Scovil
 
Static analysis for perl
Static analysis for perlStatic analysis for perl
Static analysis for perlmoznion
 

Similaire à PHP CODING STANDARDS (20)

Psr - php standards recommendations
Psr - php standards recommendationsPsr - php standards recommendations
Psr - php standards recommendations
 
Php psr standard 2014 01-22
Php psr standard 2014 01-22Php psr standard 2014 01-22
Php psr standard 2014 01-22
 
2021.laravelconf.tw.slides2
2021.laravelconf.tw.slides22021.laravelconf.tw.slides2
2021.laravelconf.tw.slides2
 
PSR: Standards in PHP by Alex Simanovich
PSR: Standards in PHP by Alex SimanovichPSR: Standards in PHP by Alex Simanovich
PSR: Standards in PHP by Alex Simanovich
 
Psr 2 coding style guide - Tidepool Labs
Psr 2   coding style guide - Tidepool LabsPsr 2   coding style guide - Tidepool Labs
Psr 2 coding style guide - Tidepool Labs
 
Presentation
PresentationPresentation
Presentation
 
args_types
args_typesargs_types
args_types
 
PHP Standards Recommendations - PHP-FIG
PHP Standards Recommendations - PHP-FIGPHP Standards Recommendations - PHP-FIG
PHP Standards Recommendations - PHP-FIG
 
Coding standards for java
Coding standards for javaCoding standards for java
Coding standards for java
 
Aura for PHP at Fossmeet 2014
Aura for PHP at Fossmeet 2014Aura for PHP at Fossmeet 2014
Aura for PHP at Fossmeet 2014
 
Theperlreview
TheperlreviewTheperlreview
Theperlreview
 
In-Depth Guide On WordPress Coding Standards For PHP & HTML
In-Depth Guide On WordPress Coding Standards For PHP & HTMLIn-Depth Guide On WordPress Coding Standards For PHP & HTML
In-Depth Guide On WordPress Coding Standards For PHP & HTML
 
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
 
Composer Helpdesk
Composer HelpdeskComposer Helpdesk
Composer Helpdesk
 
Java platform
Java platformJava platform
Java platform
 
Overview of php
Overview of phpOverview of php
Overview of php
 
PHP Course (Basic to Advance)
PHP Course (Basic to Advance)PHP Course (Basic to Advance)
PHP Course (Basic to Advance)
 
Objective-C to Swift - Swift Cloud Workshop 3
Objective-C to Swift - Swift Cloud Workshop 3Objective-C to Swift - Swift Cloud Workshop 3
Objective-C to Swift - Swift Cloud Workshop 3
 
PHP Development Tools
PHP  Development ToolsPHP  Development Tools
PHP Development Tools
 
Static analysis for perl
Static analysis for perlStatic analysis for perl
Static analysis for perl
 

PHP CODING STANDARDS

  • 1. PHP Coding Standards For Developers Presented On 13-Feb-2014 By, Sangeeta Arora – Technical Lead | CIPL sangeeta.arora@classicinformatics.com 1 This is private and confidential material
  • 2. Agenda Introduce PSR   What is PSR?  Different types of PSR Introduce DocBlocks   What is DocBlock?  Different DocBlock Tags Introduce PHP_CodeSniffer   2 What is PHP_CodeSniffer? This is private and confidential material
  • 3. PSR? PHP Standards Recommendation.  PSR stands for  Coding standards are guidelines for code style and documentation. Purpose of coding standards is to make it so that source code looks as if it’s written by a single developer as it sets a level of expectations for contributing developers. Five Types of PSR  PSR – 0 – Autoloading Standard  PSR – 1 – Basic Coding Standard  PSR – 2 – Coding Style Guide  PSR – 3 – Logger Interface  PSR – 4 – Improved Autoloading We will discuss PSR – 0, PSR – 1 and PSR – 2 in the further slides. We will discuss PSR – 3 and PSR – 4 in future presentations.     3 This is private and confidential material
  • 4. PSR – 0 – Autoloading Standard A standard recommendation that describes “the mandatory requirements that must be adhered to for autoloader interoperability. 4 This is private and confidential material
  • 5. PSR – 0 – Autoloading Standard  A fully-qualified namespace and class must have the following structure <Vendor Name>(<Namespace>)*<Class Name>.  Each namespace must have a top-level namespace ("Vendor Name").  Each namespace can have as many sub-namespaces as it wishes.  Each namespace separator is converted to a DIRECTORY_SEPARATOR when loading from the file system.  Each _ character in the CLASS NAME is converted to a DIRECTORY_SEPARATOR. The _ character has no special meaning in the namespace.  The fully-qualified namespace and class is suffixed with .php when loading from the file system.  Alphabetic characters in vendor names, namespaces, and class names may be of any combination of lower case and upper case. 5 This is private and confidential material
  • 6. PSR – 0 – Examples  DoctrineCommonIsolatedClassLoader => /path/to/project/lib/vendor/Doctrine/Common/ IsolatedClassLoader.php  SymfonyCoreRequest => /path/to/project/lib/vendor/Symfony/Core/Request.php.  ZendAcl => /path/to/project/lib/vendor/Zend/Acl.php.  ZendMailMessage => /path/to/project/lib/vendor/Zend/Mail/Message.php. 6 This is private and confidential material
  • 7. PSR – 1 – Basic Coding Standard It focuses on a Basic Coding Standard. 7 This is private and confidential material
  • 8. PSR – 1 – Basic Coding Standard  Files MUST use only <?php and <?= tags.  Files MUST use only UTF-8 (without BOM) character encoding for PHP code.  Files SHOULD either declare symbols (classes, functions, constants, etc.) or cause sideeffects (e.g. generate output, change .ini settings, etc.) but SHOULD NOT do both.  Namespaces and classes MUST follow PSR-0.  Class names MUST be declared in StudlyCaps.  Class constants MUST be declared in all upper case with underscore separators, if required. E.g. const VERSION; const DATE_APPROVED;  Class method names MUST be declared in camelCase. 8 This is private and confidential material
  • 9. PSR – 2 – Coding Style Guide Its purpose is to have a single style guide for PHP code that results in uniformly formatted shared code. 9 This is private and confidential material
  • 10. PSR – 2 – Coding Style Guide  Basic Coding Standard   Code MUST follow PSR-1. Files   All PHP files MUST end with a single blank line.   All PHP files MUST use the Unix LF (linefeed) line ending. The closing ?> tag MUST be omitted from files containing only PHP. Lines  There MUST NOT be a hard limit on line length.  The soft limit MUST be 120 characters; lines SHOULD be 80 characters or less.  There MUST NOT be trailing whitespace at the end of non-blank lines.  Blank lines MAY be added to improve readability and to indicate related blocks of code.  There MUST NOT be more than one statement per line. 10 This is private and confidential material
  • 11. PSR – 2 – Coding Style Guide …contd.  Indenting   Code MUST use 4 spaces for indenting, and MUST NOT use tabs for indenting. Keywords and True/False/Null    PHP keywords MUST be in lower case. The PHP constants true, false, and null MUST be in lower case. Namespace and Use Declarations  There MUST be one blank line after the namespace declaration.  All use declarations MUST go after the namespace declaration.  There MUST be one use keyword per declaration..  There MUST be one blank line after the use block. 11 This is private and confidential material
  • 12. PSR – 2 – Coding Style Guide …contd.  Extends and Implements  The extends and implements keywords MUST be declared on the same line as the class name. e.g.  Lists of implements MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first item in the list MUST be on the next line, and there MUST be only one interface per line. Opening braces for classes MUST go on the next line, and closing braces MUST go on the next line after the body. Properties  Visibility MUST be declared on all properties.   12 This is private and confidential material
  • 13. PSR – 2 – Coding Style Guide …contd.  Properties   The var keyword MUST NOT be used to declare a property.   There MUST NOT be more than one property declared per statement. Property names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility. e.g. Methods  Visibility MUST be declared on all methods.  Method names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility.  Method names MUST NOT be declared with a space after the method name.  The opening brace MUST go on its own line, and the closing brace MUST go on the next line following the body. 13 This is private and confidential material
  • 14. PSR – 2 – Coding Style Guide …contd.  Methods   There MUST NOT be a space after the opening parenthesis, and there MUST NOT be a space before the closing parenthesis. e.g. Method Arguments  There MUST NOT be a space before each comma.  There MUST be one space after each comma.  Method arguments with default values MUST go at the end of the argument list. 14 This is private and confidential material
  • 15. PSR – 2 – Coding Style Guide …contd.  Method Arguments Example of Arguments in the same line Example of Arguments not in the same line, when have a long list of arguments. 15 This is private and confidential material
  • 16. PSR – 2 – Coding Style Guide …contd.  Qualifiers (abstract, final, and static)  When present, the abstract and final declarations MUST precede the visibility declaration.  16 When present, the static declaration MUST come after the visibility declaration. This is private and confidential material
  • 17. PSR – 2 – Coding Style Guide …contd.  Method and Function Calls  There MUST NOT be a space between the method or function name and the opening parenthesis.  There MUST NOT be a space after the opening parenthesis.  There MUST NOT be a space before the closing parenthesis.  In the argument list, there MUST NOT be a space before each comma, and there MUST be one space after each comma. Example of Arguments in the same line 17 Example of Arguments not in the same line, when have a long list of arguments. This is private and confidential material
  • 18. PSR – 2 – Coding Style Guide …contd.  Control Structures  There MUST be one space after the control structure keyword.  There MUST NOT be a space after the opening parenthesis.  There MUST NOT be a space before the closing parenthesis.  There MUST be one space between the closing parenthesis and the opening brace.  The structure body MUST be indented once.  The closing brace MUST be on the next line after the body. 18 This is private and confidential material
  • 19. PSR – 2 – Coding Style Guide …contd.  if-elseif-else  The keyword elseif SHOULD be used instead of else if.  else and elseif are on the same line as the closing brace from the earlier body.  Note the placement of parentheses, spaces, and braces. 19 This is private and confidential material
  • 20. PSR – 2 – Coding Style Guide …contd.  switch and case  The case statement MUST be indented once from switch.  The break keyword (or other terminating keyword) MUST be indented at the same level as the case body.  There MUST be a comment such as // no break when fall-through is intentional in a nonempty case body.  Note the placement of parentheses, spaces, and braces. 20 This is private and confidential material
  • 21. PSR – 2 – Coding Style Guide …contd.  while and do-while  Note the placement of parentheses, spaces, and braces for while statement.  Note the placement of parentheses, spaces, and braces for do-while statement. 21 This is private and confidential material
  • 22. PSR – 2 – Coding Style Guide …contd.  for and foreach  Note the placement of parentheses, spaces, and braces for for statement.  Note the placement of parentheses, spaces, and braces for foreach statement. 22 This is private and confidential material
  • 23. PSR – 2 – Coding Style Guide …contd.  try-catch  23 Note the placement of parentheses, spaces, and braces for try-catch statement. This is private and confidential material
  • 24. PSR – 2 – Coding Style Guide …contd.  In-Code Documentation  All PHP files should include a DocBlock header.  All classes should include a DocBlock for the class definition.  All class constants, properties and methods should include a DocBlock to explain their purpose.  All functions should include a DocBlock to explain their purpose. 24 This is private and confidential material
  • 25. PSR – 3 – Logger Interface PSR-3 describes a shared logging interface which improves reusability. 25 This is private and confidential material
  • 26. PSR – 4 – Improved Autoloading The main goal of PSR-4 is to remove the remnants of PSR-0 and the pre-5.3 days completely and allow for a more concise folder structure. 26 This is private and confidential material
  • 27. DocBlocks DocBlock is a multi-line C-style comment used to document a block of code. 27 This is private and confidential material
  • 28. DocBlocks  It is a multi-line C-style comment used to document a block of code.  "DocComment" is a special type of comment which MUST  It begins with the character sequence /** followed by a whitespace character  It has an asterisk at the start of each line. end with */ have zero or more lines in between    DocBlocks consist of three sections:   short description long description tags  Short description is a concise description terminated by either a new-line or a period.  Long description is where the bulk of the documentation goes; it can be multi-line and as long you wish. 28 This is private and confidential material
  • 29. DocBlocks …contd.  Tags  The tags are used to specify additional information, such as the expected parameters and their type.  Each tag starts on a new line, followed by an at-sign (@) and a tag-name followed by white-space and meta-data (including a description). 29 This is private and confidential material
  • 30. DocBlocks …contd.  Different Types of Tags                      30 @api @author @copyright @deprecated @example @global @internal @license @method @package @param @property @return @see @since @struct @throws @todo @type @uses @version This is private and confidential material
  • 31. DocBlocks …contd.  @api – It is used to declare "Structural Elements" as being suitable for consumption by third parties.  @author – The @author tag is used to document the author of any "Structural Element".  @copyright – The @copyright tag is used to document the copyright information of any "Structural Element". 31 This is private and confidential material
  • 32. DocBlocks …contd.  @deprecated – The @deprecated tag is used to indicate which 'Structural elements' are deprecated and are to be removed in a future version.  @example – The @example tag is used to link to an external source code file which contains an example of use for the current "Structural element". 32 This is private and confidential material
  • 33. DocBlocks …contd.  @global – The @global tag is used to denote a global variable or its usage.  @license – The @license tag is used to indicate which license is applicable for the associated 'Structural Elements'.  @method – The @method allows a class to know which 'magic' methods are callable. 33 This is private and confidential material
  • 34. DocBlocks …contd.  @package – The @package tag is used to categorize "Structural Elements" into logical subdivisions.  @param – The @param tag is used to document a single parameter of a function or method.  @property – The @property tag is used in the situation where a class contains the __get() and __set() magic methods and allows for specific names. 34 This is private and confidential material
  • 35. DocBlocks …contd.  @return – The @return tag is used to document the return value of functions or methods.  @see – The @see tag can be used to define a reference to other "Structural Elements" or to a URI. 35 This is private and confidential material
  • 36. DocBlocks …contd.  @since – The @since tag is used to denote when an element was introduced or modified, using some description of "versioning" to that element.  The @since tag SHOULD NOT be used to show the current version of an element, the @version tag MAY be used for that purpose.  @todo – The @todo tag is used to indicate whether any development activities should still be executed on associated "Structural Elements". 36 This is private and confidential material
  • 37. DocBlocks …contd.  @throws – The @throws tag is used to indicate whether "Structural Elements" throw a specific type of exception.  @version – The @version tag is used to denote some description of "versioning" to an element. 37 This is private and confidential material
  • 38. DocBlocks …contd.  @type – The @type tag defines which type of data is represented by a value of a Constant, Property or Variable.  @uses – The @uses tag describes whether any part of the associated "Structural Element" uses, or consumes, another "Structural Element" or a file that is situated in the current project. 38 This is private and confidential material
  • 39. PHP_CodeSniffer 39 This is private and confidential material
  • 40. What is PHP_CodeSniffer?  PHP_CodeSniffer (PHPCS) is a PHP5 script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard.  It is an essential development tool that ensures your code remains clean and consistent.  It can also help prevent some common semantic errors made by developers.  It allows you to not only ensure that your code follows the PSR standards, but it also uses PHP’s linter to check for syntax errors.  Multiple coding standards can be used within PHP_CodeSniffer so that the one installation can be used across multiple projects.  The default coding standard used by PHP_CodeSniffer is the PEAR coding standard. 40 This is private and confidential material
  • 41. Q&A 41 This is private and confidential material
  • 42. Thank You 42 This is private and confidential material