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
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)
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