Top Banner
BEST PRACTICES For IT Teams and PHP DEVS
24

Coding Best practices (PHP)

Apr 13, 2017

Download

Technology

Christian Baune
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Coding Best practices (PHP)

BEST PRACTICESFor IT Teams

and

PHP DEVS

Page 2: Coding Best practices (PHP)

ControllersShould

BeSkin

Page 3: Coding Best practices (PHP)

Abuse SERVICES

Page 4: Coding Best practices (PHP)

ABUSE SERVICES

Page 5: Coding Best practices (PHP)

CONTROLLERSTOO

Page 6: Coding Best practices (PHP)
Page 7: Coding Best practices (PHP)

IOC TIME

Page 8: Coding Best practices (PHP)

RECEIVE AND DON’T

ASK

Page 9: Coding Best practices (PHP)

AVOID- new -

Page 10: Coding Best practices (PHP)

BAD, BAD, BAD

Page 11: Coding Best practices (PHP)

GOOD

Page 12: Coding Best practices (PHP)

BETTER

Page 13: Coding Best practices (PHP)

Taht’s all we really have to know

Page 14: Coding Best practices (PHP)

SECURITY

Page 15: Coding Best practices (PHP)

XSS

Page 16: Coding Best practices (PHP)

Sanitize input

URLs: url_encodeValue attribute (html): html_special_chars

See: https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet

Page 17: Coding Best practices (PHP)

SQL INJECTION

Page 18: Coding Best practices (PHP)

Dependency injection is nice, SQL injection not soALWAYS use bound parameters

IF you need to build SQL Queries, use a builder. Don’t “roll your own”

Use PDO.

Use PDO::quote to escape literals in `IN` clause. If these are numbers, use `intval()` or

`floatval`.

Do not trust data, even from database.

Page 19: Coding Best practices (PHP)

Other security tips● Use secure cookies (http://cookiecontroller.com/internet-cookies/secure-cookies/)

● Sign your cookies & encrypt them !

(httpOnly & secure attributes + hmac signature & AES encryption)

● Check on UI and backend

(Hiding a button is not enough to prevent an action)

Page 20: Coding Best practices (PHP)

UNSORTED

Page 21: Coding Best practices (PHP)

Know your stuff● DO IT RIGHT : www.phptherightway.com

● DO IT SECURE : https://www.owasp.org/

● RTFM : http://be2.php.net/manual/en/

● CS can help : https://sourcemaking.com/

Page 22: Coding Best practices (PHP)

Teams are smarter than individuals● Reuse components

○ http://symfony.com/components

○ http://www.yiiframework.com/extensions/

● Don’t reinvent the wheel

○ Involve standards

■ https://tools.ietf.org/

■ http://www.php-fig.org/psr/

■ https://www.jcp.org/en/jsr/overview (yes, you can borrow from other technos!)

● Don’t re-implement the framework

○ Eg. $_SERVER[‘REQUEST_METHOD’]==’POST’ ? $repo->save($user) : $repo->get($user->id)

● Don’t misuse framework hooks (Eg. save entities in a “validate” method)

Page 23: Coding Best practices (PHP)

Handle error and unusual activity properly● Log odd events with at least a “WARNING” level;

● Throw exceptions on exceptional situations;

○ Create your own exceptions unless you can reuse an existing one;

○ Log details which can help debugging;

● With good logging, reading the code becomes optional;

● Do not attempt to “automagically” fix some “bad call”

○ If you don’t know : good place for throwing an exception !

● Validate input on public methods;

● All “switch” have to feature a “default” case;

● Bail out as early as possible; (if ... return)

Page 24: Coding Best practices (PHP)

Tricks● Feel compelled to make a comment ? → make a function !

● Too many indents ?→ make a function or bail out early !

● Using break ? → make a function !

● Need to inherit more than one class ? → use composition !

● Too many controller dependencies ? → split your controller !

● Code hard to read ? → good naming, functions !

● Troubles to use a class ?→ Don’t use magic methods (__get, __invoke, …) !

(Magic methods should be used to make proxies and advanced stuff)