際際滷

際際滷Share a Scribd company logo
PHP Standards
Recommendations
(PSR)
Akshay Khale

(Sr. Software Engineer)
C Martin Fowler
^Any fool can write code that a computer can
understand. Good programmers write code that
humans can understand. ̄
PSR-1: Basic Coding
Standards.
? MUST use only <?php and <?= tags.

? MUST use only UTF-8 Encoding.

? SHOULD either declare symbols or cause side-e?ects.

? Class names MUST be declared in CamelCase or
StudlyCaps.

? constants MUST be declared in all UPPER_SNAKE_CASE.

? Method names MUST be declared in camelCase.
Quiz Time: What is wrong with
following code snippets?
PSR-2: Coding Style Guide
? 4 Space indenting (No Tabs).

? Soft Line limit 120 Characters.

? Blank space after namespace declaration before use.

? Closing Brace ^} ̄ always on a separate line.

? Opening Brace ^{ ̄ Same line (with a space before) for control statements
and next line for Classes and functions.

? Space before open parentheses ^( ̄ for control statements and not for
functions.

? Visibility mode is must for all properties and methods. abstract and ?nal
before visibility and static after the visibility.
Quiz Time: What is wrong with
following code snippets?
PSR-3: Logger Interface
? Only allowed Log levels: debug, info, notice, warning, error,
critical, alert, emergency.

? MUST implement PsrLogLoggerInterface.

? MUST throw PsrLogInvalidArgumentException exception
on invalid log level.

? public function log($level, $message, array $context =
array());
? One can consider extending PsrLogAbstractLogger class
which implements LoggerInterface.
Sample Logger Class:
https://github.com/akshaykhale1992/luxio-logger
PSR-4: Autoloader
? Namespace: <vendor_namespace>/
<package_namespace>[/<subnamespace>/]/
<class_name>.

? Folder structure should also follow the namespace
pattern.
PSR-6: Caching Interface
? MUST implement PsrCacheCacheItemPoolInterface and
PsrCacheCacheItemInterface.

? Concepts: Pool, Items, Key, Data, TTL, Hit, Miss.

? Should not throw any Exception.
PSR-7: HTTP message
interfaces
? Request Structure

? Response Structure

? Interfaces: PsrHttpMessageRequestInterface and
PsrHttpMessageResponseInterface.
PSR-11: Container interface
? MUST implement PsrContainerContainerInterface.

? MUST throw PsrContainerNotFoundExceptionInterface
exception if object not found.

? Directly thrown exceptions must implement
PsrContainerContainerExceptionInterface interface.
Standards that most of the
developers follow.
? If a ?le de?nes a Class then it should not have any other declarations though PSR
allows.

? Files with functions should have only function de?nitions.

? Files with class / functions de?nitions should not have a closing PHP Tag (?>) and
should have an empty line before EOF.

? Helper ?les must have function_exists check.

? Code MUST be correctly formatted based on the PSR rules. (editor plugins can be used
for the same).

? Arrays properties should be on separate lines.

? All declarations MUST have dock-blocks.

? Branch names MUST follow bitbucket Branching Model.
Cont.
? Use static instead of self for calling static class members.

? Use die instead of exit.

? Make use of destructors for unsetting objects and/or closing opened ?les.

? There should not be any commented code or commented die / dd statements.

? SHOULD avoid else block in case of if statements.

? Recommended to make use of exceptions in-case of invalid details which should be
handles at calling end.

? Try to use User de?ned constants instead of System constants. A constants class should
be created for de?ning all the constants that are being used in the project. E.g. instead of
using ^/ ̄ for directory paths use DIRECTORY_SEPARATOR or instead of using supplier_id
== 7 check use supplier_id == Constants::DELHIVERY_SUPPLIER_ID.

? PHP Unit Test cases are compulsory.
Samples:
Thank you!!!

More Related Content

PHP Standards Recommendations - PHP-FIG

  • 2. C Martin Fowler ^Any fool can write code that a computer can understand. Good programmers write code that humans can understand. ̄
  • 3. PSR-1: Basic Coding Standards. ? MUST use only <?php and <?= tags. ? MUST use only UTF-8 Encoding. ? SHOULD either declare symbols or cause side-e?ects. ? Class names MUST be declared in CamelCase or StudlyCaps. ? constants MUST be declared in all UPPER_SNAKE_CASE. ? Method names MUST be declared in camelCase.
  • 4. Quiz Time: What is wrong with following code snippets?
  • 5. PSR-2: Coding Style Guide ? 4 Space indenting (No Tabs). ? Soft Line limit 120 Characters. ? Blank space after namespace declaration before use. ? Closing Brace ^} ̄ always on a separate line. ? Opening Brace ^{ ̄ Same line (with a space before) for control statements and next line for Classes and functions. ? Space before open parentheses ^( ̄ for control statements and not for functions. ? Visibility mode is must for all properties and methods. abstract and ?nal before visibility and static after the visibility.
  • 6. Quiz Time: What is wrong with following code snippets?
  • 7. PSR-3: Logger Interface ? Only allowed Log levels: debug, info, notice, warning, error, critical, alert, emergency. ? MUST implement PsrLogLoggerInterface. ? MUST throw PsrLogInvalidArgumentException exception on invalid log level. ? public function log($level, $message, array $context = array()); ? One can consider extending PsrLogAbstractLogger class which implements LoggerInterface.
  • 9. PSR-4: Autoloader ? Namespace: <vendor_namespace>/ <package_namespace>[/<subnamespace>/]/ <class_name>. ? Folder structure should also follow the namespace pattern.
  • 10. PSR-6: Caching Interface ? MUST implement PsrCacheCacheItemPoolInterface and PsrCacheCacheItemInterface. ? Concepts: Pool, Items, Key, Data, TTL, Hit, Miss. ? Should not throw any Exception.
  • 11. PSR-7: HTTP message interfaces ? Request Structure ? Response Structure ? Interfaces: PsrHttpMessageRequestInterface and PsrHttpMessageResponseInterface.
  • 12. PSR-11: Container interface ? MUST implement PsrContainerContainerInterface. ? MUST throw PsrContainerNotFoundExceptionInterface exception if object not found. ? Directly thrown exceptions must implement PsrContainerContainerExceptionInterface interface.
  • 13. Standards that most of the developers follow. ? If a ?le de?nes a Class then it should not have any other declarations though PSR allows. ? Files with functions should have only function de?nitions. ? Files with class / functions de?nitions should not have a closing PHP Tag (?>) and should have an empty line before EOF. ? Helper ?les must have function_exists check. ? Code MUST be correctly formatted based on the PSR rules. (editor plugins can be used for the same). ? Arrays properties should be on separate lines. ? All declarations MUST have dock-blocks. ? Branch names MUST follow bitbucket Branching Model.
  • 14. Cont. ? Use static instead of self for calling static class members. ? Use die instead of exit. ? Make use of destructors for unsetting objects and/or closing opened ?les. ? There should not be any commented code or commented die / dd statements. ? SHOULD avoid else block in case of if statements. ? Recommended to make use of exceptions in-case of invalid details which should be handles at calling end. ? Try to use User de?ned constants instead of System constants. A constants class should be created for de?ning all the constants that are being used in the project. E.g. instead of using ^/ ̄ for directory paths use DIRECTORY_SEPARATOR or instead of using supplier_id == 7 check use supplier_id == Constants::DELHIVERY_SUPPLIER_ID. ? PHP Unit Test cases are compulsory.