SlideShare a Scribd company logo
1 of 25
Download to read offline
PSR-2:
Coding Style Guide
PSR-2's purpose is to have a single
style guide for PHP code that
results in uniformly formatted
shared code.
● PSR-2 is a coding style agreed upon by many
PHP framework developers
● It’s an extension of the original PSR-1,
which basically has 3 main conventions.
● The benefit of PSR-2 is not in the rules
themselves, but in the sharing of those
rules.
3 Main Conventions of PSR-1
● Classes are upper CamelCase.
● Methods are lower camelCase.
● Constants are written in caps like
VERSION_NUMBER.
Some other rules of PSR-1
● Files MUST use only <?php and <?= tags.
● Files MUST use only UTF-8 without BOM for
PHP code.
● Class names MUST be declared in StudlyCaps
(text notation in which the capitalization of letters
varies by some pattern, or arbitrarily, usually also
omitting spaces between words and often omitting some
letters).
etc...
PSR-2 expands PSR-1 by using these
standards:
● Indents are 4 spaces instead of tabs.
● All PHP files MUST end with a single blank line.
● The closing ?> tag MUST be omitted from files
containing only PHP.
● Keep line lengths at 80 characters. Soft limit of
120 characters.
● Use one blank line after a namespace declaration.
● Use a blank line after a group of declarations.
● There MUST NOT be trailing whitespace at the end of
non-blank lines.
● The `extends` and `implements` keywords MUST be
declared on the same line as the class name.
● Visibility MUST be declared on all methods.
● In the argument list, there MUST NOT be a space before
each comma, and there MUST be one space after each
comma.
● Opening braces for classes MUST go on the next line.
Closing brace must be on its own line.
● Opening braces for methods must go on the next line,
and a separate line for the closing brace.
● Closures must have a space after the function keyword.
● Never use the var keyword, this is used with
JavaScript and would cause some confusion.
● Control structures, like IF, WHICH, FOR, and FOREACH
must have one space before the condition parenthesis.
● The case keywords should be indented from the switch
keyword.
● Keywords, like true, false, and null should be in
lowercase.
Examples
that show how to
use PSR-2
CLASS
METHOD
PROPERTYWrong
Right
METHOD ARGUMENTS
CONTROL:
one space before the condition parenthesis
SWITCH
WHILE, DO WHILE
KEYWORD
PHP Mess Detector
(PHPMD)
What PHPMD does?
It takes a given PHP source code
base and look for several potential
problems within that source.
● PHPMD works through Rules – specific sets of
instructions on what to look for in source code
● There’s a wide variety of prepared rules and
users can write their own
● PHPMD can be installed globally via composer by
running
composer global require 'phpmd/phpmd=*'
Potential Problems within
the Source:
● Possible bugs
● Suboptimal code
● Overcomplicated expressions
● Unused parameters, methods, properties
etc.
Some examples of rules:
● Clean Code Rules: enforce a clean code base.
● Naming Rules: about names - too long, too short,
and so forth
● Unused Code Rules: finds unused code.
● ElseExpression: An if expression with an else
branch is never necessary. You can rewrite the
conditions in a way that the else is not necessary
and the code becomes simpler to read. To achieve
this use early return statements.
● ExcessiveMethodLength: Try to reduce the method
size by creating helper methods and removing
any copy/pasted code.
● ExcessiveClassLength: Try to break it down, and
reduce the size to something manageable.
● ExcessiveParameterList: Long parameter lists
can indicate that a new object should be
created to wrap the numerous parameters.
● ExcessivePublicCount: A large number of public
methods and attributes declared in a class can
indicate the class may need to be broken up.
● CamelCaseClassName: It is considered best practice
to use the CamelCase notation to name classes.
● CamelCasePropertyName: use the camelCase notation
to name attributes.
● CamelCaseMethodName: use the camelCase notation to
name methods.
● CamelCaseParameterName: use the camelCase notation
to name parameters.
● CamelCaseVariableName: use the camelCase notation
to name variables.
● NumberOfChildren: A class with an excessive number of
children is an indicator for an unbalanced class
hierarchy. You should consider to refactor this class
hierarchy.
● DepthOfInheritance: A class with many parents is an
indicator for an unbalanced and wrong class hierarchy.
● ShortVariable: Detects when a field, local, or
parameter has a very short name.
● LongVariable: Detects when a field, formal or local
variable is declared with a long name.
● ShortMethodName: Detects when very short method names
are used.
● ConstantNamingConventions: Class/Interface constant
names should always be defined in uppercase.

More Related Content

Similar to Psr 2 coding style guide - Tidepool Labs

Md06 advance class features
Md06 advance class featuresMd06 advance class features
Md06 advance class featuresRakesh Madugula
 
c-coding-standards-and-best-programming-practices.ppt
c-coding-standards-and-best-programming-practices.pptc-coding-standards-and-best-programming-practices.ppt
c-coding-standards-and-best-programming-practices.pptVinayakHospet1
 
Presentation
PresentationPresentation
Presentationbugway
 
Hsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdfHsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdfAAFREEN SHAIKH
 
An intro to php standards recommendation (psr)
An intro to php standards recommendation (psr)An intro to php standards recommendation (psr)
An intro to php standards recommendation (psr)valuebound
 
Server Scripting Language -PHP
Server Scripting Language -PHPServer Scripting Language -PHP
Server Scripting Language -PHPDeo Shao
 
Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationAkhil Kaushik
 
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
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytechyannick grenzinger
 
Airbnb Javascript Style Guide
Airbnb Javascript Style GuideAirbnb Javascript Style Guide
Airbnb Javascript Style GuideCreative Partners
 
Best practices in enterprise applications
Best practices in enterprise applicationsBest practices in enterprise applications
Best practices in enterprise applicationsChandra Sekhar Saripaka
 
New c sharp4_features_part_i
New c sharp4_features_part_iNew c sharp4_features_part_i
New c sharp4_features_part_iNico Ludwig
 
New c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_iiNew c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_iiNico Ludwig
 
Lecture: Refactoring
Lecture: RefactoringLecture: Refactoring
Lecture: RefactoringMarcus Denker
 
GTU PHP Project Training Guidelines
GTU PHP Project Training GuidelinesGTU PHP Project Training Guidelines
GTU PHP Project Training GuidelinesTOPS Technologies
 

Similar to Psr 2 coding style guide - Tidepool Labs (20)

Md06 advance class features
Md06 advance class featuresMd06 advance class features
Md06 advance class features
 
c-coding-standards-and-best-programming-practices.ppt
c-coding-standards-and-best-programming-practices.pptc-coding-standards-and-best-programming-practices.ppt
c-coding-standards-and-best-programming-practices.ppt
 
Presentation
PresentationPresentation
Presentation
 
Hsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdfHsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdf
 
Lambdas in Java 8
Lambdas in Java 8Lambdas in Java 8
Lambdas in Java 8
 
An intro to php standards recommendation (psr)
An intro to php standards recommendation (psr)An intro to php standards recommendation (psr)
An intro to php standards recommendation (psr)
 
Server Scripting Language -PHP
Server Scripting Language -PHPServer Scripting Language -PHP
Server Scripting Language -PHP
 
Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code Generation
 
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
 
Java
JavaJava
Java
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytech
 
overview of c#
overview of c#overview of c#
overview of c#
 
Annotations
AnnotationsAnnotations
Annotations
 
Airbnb Javascript Style Guide
Airbnb Javascript Style GuideAirbnb Javascript Style Guide
Airbnb Javascript Style Guide
 
Best practices in enterprise applications
Best practices in enterprise applicationsBest practices in enterprise applications
Best practices in enterprise applications
 
New c sharp4_features_part_i
New c sharp4_features_part_iNew c sharp4_features_part_i
New c sharp4_features_part_i
 
New c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_iiNew c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_ii
 
Lecture: Refactoring
Lecture: RefactoringLecture: Refactoring
Lecture: Refactoring
 
Java script basics
Java script basicsJava script basics
Java script basics
 
GTU PHP Project Training Guidelines
GTU PHP Project Training GuidelinesGTU PHP Project Training Guidelines
GTU PHP Project Training Guidelines
 

Recently uploaded

Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniquesugginaramesh
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 

Recently uploaded (20)

Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniques
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 

Psr 2 coding style guide - Tidepool Labs

  • 2. PSR-2's purpose is to have a single style guide for PHP code that results in uniformly formatted shared code.
  • 3. ● PSR-2 is a coding style agreed upon by many PHP framework developers ● It’s an extension of the original PSR-1, which basically has 3 main conventions. ● The benefit of PSR-2 is not in the rules themselves, but in the sharing of those rules.
  • 4. 3 Main Conventions of PSR-1 ● Classes are upper CamelCase. ● Methods are lower camelCase. ● Constants are written in caps like VERSION_NUMBER.
  • 5. Some other rules of PSR-1 ● Files MUST use only <?php and <?= tags. ● Files MUST use only UTF-8 without BOM for PHP code. ● Class names MUST be declared in StudlyCaps (text notation in which the capitalization of letters varies by some pattern, or arbitrarily, usually also omitting spaces between words and often omitting some letters). etc...
  • 6. PSR-2 expands PSR-1 by using these standards: ● Indents are 4 spaces instead of tabs. ● All PHP files MUST end with a single blank line. ● The closing ?> tag MUST be omitted from files containing only PHP. ● Keep line lengths at 80 characters. Soft limit of 120 characters. ● Use one blank line after a namespace declaration. ● Use a blank line after a group of declarations.
  • 7. ● There MUST NOT be trailing whitespace at the end of non-blank lines. ● The `extends` and `implements` keywords MUST be declared on the same line as the class name. ● Visibility MUST be declared on all methods. ● In the argument list, there MUST NOT be a space before each comma, and there MUST be one space after each comma.
  • 8. ● Opening braces for classes MUST go on the next line. Closing brace must be on its own line. ● Opening braces for methods must go on the next line, and a separate line for the closing brace. ● Closures must have a space after the function keyword. ● Never use the var keyword, this is used with JavaScript and would cause some confusion. ● Control structures, like IF, WHICH, FOR, and FOREACH must have one space before the condition parenthesis. ● The case keywords should be indented from the switch keyword. ● Keywords, like true, false, and null should be in lowercase.
  • 9. Examples that show how to use PSR-2
  • 10. CLASS
  • 14. CONTROL: one space before the condition parenthesis
  • 19. What PHPMD does? It takes a given PHP source code base and look for several potential problems within that source.
  • 20. ● PHPMD works through Rules – specific sets of instructions on what to look for in source code ● There’s a wide variety of prepared rules and users can write their own ● PHPMD can be installed globally via composer by running composer global require 'phpmd/phpmd=*'
  • 21. Potential Problems within the Source: ● Possible bugs ● Suboptimal code ● Overcomplicated expressions ● Unused parameters, methods, properties etc.
  • 22. Some examples of rules: ● Clean Code Rules: enforce a clean code base. ● Naming Rules: about names - too long, too short, and so forth ● Unused Code Rules: finds unused code. ● ElseExpression: An if expression with an else branch is never necessary. You can rewrite the conditions in a way that the else is not necessary and the code becomes simpler to read. To achieve this use early return statements.
  • 23. ● ExcessiveMethodLength: Try to reduce the method size by creating helper methods and removing any copy/pasted code. ● ExcessiveClassLength: Try to break it down, and reduce the size to something manageable. ● ExcessiveParameterList: Long parameter lists can indicate that a new object should be created to wrap the numerous parameters. ● ExcessivePublicCount: A large number of public methods and attributes declared in a class can indicate the class may need to be broken up.
  • 24. ● CamelCaseClassName: It is considered best practice to use the CamelCase notation to name classes. ● CamelCasePropertyName: use the camelCase notation to name attributes. ● CamelCaseMethodName: use the camelCase notation to name methods. ● CamelCaseParameterName: use the camelCase notation to name parameters. ● CamelCaseVariableName: use the camelCase notation to name variables.
  • 25. ● NumberOfChildren: A class with an excessive number of children is an indicator for an unbalanced class hierarchy. You should consider to refactor this class hierarchy. ● DepthOfInheritance: A class with many parents is an indicator for an unbalanced and wrong class hierarchy. ● ShortVariable: Detects when a field, local, or parameter has a very short name. ● LongVariable: Detects when a field, formal or local variable is declared with a long name. ● ShortMethodName: Detects when very short method names are used. ● ConstantNamingConventions: Class/Interface constant names should always be defined in uppercase.