psr - php standards recommendations

27

Upload: ha-anh-son

Post on 24-Jan-2018

358 views

Category:

Engineering


7 download

TRANSCRIPT

Page 1: Psr - php standards recommendations
Page 2: Psr - php standards recommendations

Topics

1. SPMC Case-Study

2. PSR

Auto-loading

Logger Interface

PSR1 – Basic Coding Standards

PSR2 – Coding Style Guides

3. Tools

Trung tâm lập trình Codeto - 2015

Page 3: Psr - php standards recommendations

Case Study(1)

Case-Sensitive

PSEmployee or PsEmployee ?

Windows fine - Unix die

Raise Call to Undefined Class Errors

Trung tâm lập trình Codeto - 2015

Page 4: Psr - php standards recommendations

Case Study(2)

Text Encoding

WTF are displaying? Have you ever care?

Raise incompatible character encodings errors

Trung tâm lập trình Codeto - 2015

Page 5: Psr - php standards recommendations

Case Study(3)

Page 6: Psr - php standards recommendations

PSR

PSR (PHP Standards Recommendation)

php-fig.com (PHP Framework Interop Group)

Zend 2, Symfony 2, Yii 2, AWS SDK, CakePHP, Laravel

40+ members (www.php-fig.org/members/)

PHP-FIG mission:

Finding the way of working together

http://www.php-fig.org

Page 7: Psr - php standards recommendations

NOTICE

Guide meaning?

Follow the rule STRICTLY

UNDERSTAND keywords

MUST, MUST NOT, REQUIRED, SHALL, SHALL NOT, SHOULD,

SHOULD NOT, RECOMMENDED, MAY, and OPTIONAL

(http://tools.ietf.org/html/rfc2119)

Clear the FIG & PSR PURPOSE

http://www.php-fig.org/faq/

Trung tâm lập trình Codeto - 2015

Page 8: Psr - php standards recommendations

http://www.php-fig.org Trung tâm lập trình Codeto - 2015

Page 9: Psr - php standards recommendations

AUTOLOADING

Standard form:

\<NamespaceName>(\<SubNamespaceNames>)*\<ClassName>

Page 10: Psr - php standards recommendations

LOGGER INTERFACES

Libraries to receive a Psr\Log\LoggerInterface object and

write logs to it in a simple and universal way

The LoggerInterface exposes eight methods to write logs

(debug, info, notice, warning, error, critical, alert, emergency).

A ninth method log(), accepts a log level as first argument.

This method MUST have the same result as calling the level-

specific method

Trung tâm lập trình Codeto - 2015

Page 11: Psr - php standards recommendations

PSR-1 Basic Coding Standard

High-level of PHP code

Files (PHP Tags, Character Encoding, Side Effects)

Namespace & Class Names (5.3 and later)

Class Constants, Properties, and Methods (Naming

Convention)

http://www.php-fig.org/psr/psr-1/Trung tâm lập trình Codeto - 2015

Page 12: Psr - php standards recommendations

Examples

Trung tâm lập trình Codeto - 2015

Page 13: Psr - php standards recommendations

Style Guide

Files, Lines

Indent, tabs and spaces

Hard and soft limit

Open/Close braces“{“ and “}”

Classes, Properties and Methods

PSR-2 Coding Style Guide

Trung tâm lập trình Codeto - 2015

Page 14: Psr - php standards recommendations

PSR-2 Overview

MUST

4 spaces indents and tabs

Soft limit 120 characters (should 80)

1 blank line after “namespace” and “use”

“{“ and “}” be next line for class and method

“{“ be the same line for control structure

public/protected/private (visibility) MUST be declared for

properties/methods

abstract/final/static before visibility

1 space after control structure keyword

Trung tâm lập trình Codeto - 2015

Page 15: Psr - php standards recommendations

Trung tâm lập trình Codeto - 2015

Page 16: Psr - php standards recommendations

PSR-2 GeneralFiles

MUST be end with single blank line and use Unix LF line ending

If PHP only, don’t use closing ?>

Lines MUST NOT hard limit, soft limit must be 120

SHOULD be 80 and must not white space at the end

MUST not more than one statement/1 line

MAY be blank line for readable

Indents MUST NOT use tabs for indents

MUST 4 spaces for indent

Keyword and True/False/Null PHP keywords MUST be lower case

Constants true, false, null MUST be lower case

Namespace and Use MUST be one blank line after “namespace” and “use” declaration

“use” MUST be after “namespace” declaration

Trung tâm lập trình Codeto - 2015

Page 17: Psr - php standards recommendations

Classes

– Extends and Implements

• “extends” and “implements” keywords MUST on the same line

• Lists of implements MAY be split multiple lines

PSR-2 Classes, Properties, and Methods

Trung tâm lập trình Codeto - 2015

Page 18: Psr - php standards recommendations

Properties

Visibility MUST be declared on ALL properties

MUST be ONE property PER statement

SHOULD be underscore (_) prefix for PROTECTED/PRIVATE

PSR-2 Classes, Properties, and Methods

Trung tâm lập trình Codeto - 2015

Page 19: Psr - php standards recommendations

Methods

Visibility MUST be declared on all methods.

SHOULD NOT prefix underscore for protected/private

MUST NOT space after the method name.

“{“ MUST go on same line,

“}” MUST go on the next line the body.

MUST NOT space after the “(“ and before “)”

PSR-2 Classes, Properties, and Methods

Trung tâm lập trình Codeto - 2015

Page 20: Psr - php standards recommendations

PSR-2 Classes, Properties, and Methods

Trung tâm lập trình Codeto - 2015

Page 21: Psr - php standards recommendations

Method Arguments

MUST NOT space before comma

MUST be one space after comma.

Arguments with default values MUST be the end of

argument list.

MAY be split multiple lines then MUST one line per

argument

PSR-2 Classes, Properties, and Methods

Trung tâm lập trình Codeto - 2015

Page 22: Psr - php standards recommendations

Trung tâm lập trình Codeto - 2015

Page 23: Psr - php standards recommendations

abstract, final, and static

abstract and static MUST before visibility

static MUST after visibility

PSR-2 Classes, Properties, and Methods

Trung tâm lập trình Codeto - 2015

Page 24: Psr - php standards recommendations

MUST be one space after control structure keyword

MUST NOT be a space after “(“

MUST NOT be a space before “)”

MUST be one space between the “)” and “{“

Structure body MUST be indented once

The closing brace MUST be on the next line after the

body

PSR-2 Control Structure

Trung tâm lập trình Codeto - 2015

Page 25: Psr - php standards recommendations

Trung tâm lập trình Codeto - 2015

Page 26: Psr - php standards recommendations

Trung tâm lập trình Codeto - 2015

Page 27: Psr - php standards recommendations

Tools

PHP Coding Standards Fixer

Check and Fix code follow PSR-1 and PSR-2

Customizable fixer (rules)

Command line

Configuration file

PHP version >= 5.3.6

http://cs.sensiolabs.org/

Trung tâm lập trình Codeto - 2015