Slide 1

Slide 1 text

Tools for Better PHP Lorna Mitchell, php|tek 2013

Slide 2

Slide 2 text

Tools for Better PHP Hi, I'm Lorna and I care about tools

Slide 3

Slide 3 text

Good Tools Matter

Slide 4

Slide 4 text

Today's Tools Development Tools • Static analysis tools • Task automation DevOps Tools • Continuous integration • Monitoring • Deployment

Slide 5

Slide 5 text

Source Control is Key

Slide 6

Slide 6 text

Static Analysis Looking at code in its own right • Evaluate files and structure • Tokenise code • Don't execute it

Slide 7

Slide 7 text

PHP Code Sniffer (phpcs) • Command-line tool, available from PEAR • CodeSniffer checks if coding standards are followed • Bracket location • Whitespace • Naming conventions • PHP doesn't care about whitespace, why should we?

Slide 8

Slide 8 text

phpcs Example scratch($enemy); return true; }

Slide 9

Slide 9 text

phpcs Example FILE: /home/lorna/.../tek13/5-tools/code/Cat.php -------------------------------------------------------- FOUND 8 ERROR(S) AFFECTING 5 LINE(S) -------------------------------------------------------- 2 | ERROR | Missing file doc comment 3 | ERROR | Missing class doc comment 5 | ERROR | Missing function doc comment 5 | ERROR | Opening brace should be on a new line 9 | ERROR | Missing function doc comment 9 | ERROR | Opening brace should be on a new line 13 | ERROR | Missing function doc comment 13 | ERROR | Opening brace should be on a new line --------------------------------------------------------

Slide 10

Slide 10 text

phpcs Example PHP CODE SNIFFER REPORT SUMMARY --------------------------------------------------------- FILE ERRORS WARNINGS --------------------------------------------------------- /home/lorna/.../code/Cat.php 8 0 --------------------------------------------------------- A TOTAL OF 8 ERROR(S) WERE FOUND IN 1 FILE(S) --------------------------------------------------------- Time: 0 seconds, Memory: 1.50Mb

Slide 11

Slide 11 text

phpcs and Jenkins

Slide 12

Slide 12 text

DIY CodeSniffer Standard • Standards are made of sniffs • There are lots of ready-defined sniffs available • You can "pick and mix" to define your own standard

Slide 13

Slide 13 text

PHPDocumentor (phpdoc) • Command-line tool, available from PEAR • PHPDocumentor creates documentation from code • Useful reference and deliverable • What if you have no comments?

Slide 14

Slide 14 text

phpdoc Example Generate documentation into the docs directory phpdoc -t docs -d .

Slide 15

Slide 15 text

phpdoc Example

Slide 16

Slide 16 text

phpdoc Example

Slide 17

Slide 17 text

phpdoc Example scratch($enemy); return true; }

Slide 18

Slide 18 text

phpdoc Example

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

phpdoc Example */ class Cat {

Slide 21

Slide 21 text

/** * The purr function, output can vary between related spec * * @return string Representation of noise made by happy ca */ public function purr() { return "prrrr"; } /** * Method for expressing dissatisfaction towards something * * @param mixed $thing The offending thing * @return boolean Whether the thing was suitably rebuffed */ public function scratch($thing) { return true;

Slide 22

Slide 22 text

} /** * Invokes combat with the other Cat * * @param Cat $enemy The cat to fight with * @return boolean Whether $this was victorious */ public function fight(Cat $enemy) { $this->scratch($enemy); return true; } }

Slide 23

Slide 23 text

phpdoc Example

Slide 24

Slide 24 text

Adding PHPDoc to your Process • Comments also help: • IDEs • PHP WSDL Generator

Slide 25

Slide 25 text

Other Static Analysis Tools • Lint check: php -l • PHP Lines of Code (phploc) • PHP Mess Detector (phpmd)

Slide 26

Slide 26 text

Build Servers and PHP

Slide 27

Slide 27 text

Build Server Many possible uses for a build server: • Run quality measure • Build docs • Deploy (more on this in a moment) • Run other phing tasks

Slide 28

Slide 28 text

CI and Jenkins Continuous integration: regular build tasks • Often in response to commit/merge Jenkins: Java-based CI tool • Also see: PPW (PHP Project Wizard)

Slide 29

Slide 29 text

Jenkins

Slide 30

Slide 30 text

Phing PHing Is Not Gnu Make • easy way to run project tasks • php specific • XML config file can be part of project

Slide 31

Slide 31 text

Example Phing build.xml

Slide 32

Slide 32 text

Automated Deployment

Slide 33

Slide 33 text

Deployment Steps: 1 Package application code. • tag/export • minify CSS, optimise images • compress

Slide 34

Slide 34 text

Deployment Steps: 2 Put code on server. • upload • uncompress to a new directory • link configuration files, upload files, etc

Slide 35

Slide 35 text

Deployment Steps: 3 (Maintenance mode if necessary) Apply database patches. • database patching strategy is vital • many good tools to help (dbdeploy, liquibase, or patch files) • code may tolerate multiple database states

Slide 36

Slide 36 text

Deployment Steps: 4 Make new code live by switching symlink. (Remove maintenance mode)

Slide 37

Slide 37 text

Deployment Steps: 5 Restart long-running processes. Sort out caches - clear/warm

Slide 38

Slide 38 text

Supervisord A process to care for your long-running processes • logs output • restarts errored scripts

Slide 39

Slide 39 text

Supervisord http://www.flickr.com/photos/megandavid/2142156

Slide 40

Slide 40 text

Supervisorctl

Slide 41

Slide 41 text

Using Supervisord Useful for: • application servers • IRC bots • workers

Slide 42

Slide 42 text

Gearman • Gearman is a job server • A job needs doing, add it to the server • Workers take and process jobs • They need a restart when we deploy new code

Slide 43

Slide 43 text

What next?

Slide 44

Slide 44 text

Monitoring • Check sites are responding well • Don't discover a problem when the client calls you Uptime Robot: http://uptimerobot.com

Slide 45

Slide 45 text

Uptime Robot

Slide 46

Slide 46 text

Good Tools Mean Good Work

Slide 47

Slide 47 text

Tools Glossary PHP Code Sniffer http://lrnja.net/17DaDvD PHPDocumentor http://phpdoc.org Jenkins http://jenkins-ci.org/ Phing http://www.phing.info/ Supervisord http://supervisord.org/ Gearman http://gearman.org/ UptimeRobot http://www.uptimerobot.com/

Slide 48

Slide 48 text

Further Reading PHP Master from Sitepoint (by @lornajane, @Davey and @Elazar)

Slide 49

Slide 49 text

Thanks! Feedback: https://joind.in/8151 • Lorna Mitchell: web development consultant • http://lornajane.net • @lornajane