Everyone talks about raising the bar on the quality of code, but it’s hard to implement when you have no clue where to start. This talk is geared toward all levels of developers, and will teach you how to improve by using the right tools effectively – a must-attend for any PHP developer who wants to scale up their quality.
Michelangelo will tell us about Quality Assurance for PHP in general and show how different QA-related actions can be performed using PhpStorm IDE. The webinar will cover topics including:
Revision control
Syntax checking
Code documentation
Unit Testing with PHPUnit
Measuring code health with a variety of tools
Profiling and debugging with Xdebug
Automation with Phing
Team work and more.
14. Advantages of SCM
• team development possible
• tracking multi-versions of source code
• moving back and forth in history
• tagging of milestones
• backup of source code
• accessible from
- command line
- native apps
- IDE’s like JetBrain’s IDE
- analytical tools
TIP:
hooks
for
tools
16. PHP
Lint
16
• checks the syntax of code
• build in PHP core
• is used per file
- pre-commit hook for version control system
- batch processing of files
• can provide reports
- but if something fails -> the build fails
TIP:
pre-‐commit
hook
18. SCM
commit
hook
18
#!/bin/sh
#
# Pre-commit hook to validate syntax of incoming PHP files, if no failures it
# accepts the commit, otherwise it fails and blocks the commit
REPOS="$1"
TXN="$2"
# modify these system executables to match your system
PHP=/usr/bin/php
AWK=/usr/bin/awk
GREP=/bin/grep
SVNLOOK=/usr/bin/svnlook
# PHP Syntax checking with PHP Lint
# originally from Joe Stump at Digg
# https://gist.github.com/53225
#
for i in `$SVNLOOK changed -t "$TXN" "$REPOS" | $AWK '{print $2}'`
do
if [ ${i##*.} == php ]; then
CHECK=`$SVNLOOK cat -t "$TXN" "$REPOS" $i | $PHP -d html_errors=off -l || echo $i`
RETURN=`echo $CHECK | $GREP "^No syntax" > /dev/null && echo TRUE || echo FALSE`
if [ $RETURN = 'FALSE' ]; then
echo $CHECK 1>&2;
exit 1
fi
fi
done
22. Why
documenIng?
22
• new
members
in
the
team
• working
with
remote
workers
• analyzing
improvements
• think
before
doing
• used
by
IDE’s
and
editors
for
code
hinIng
;-‐)
40. WriIng
a
test
40
<?php
class Application_Model_CommentTest extends PHPUnit_Framework_TestCase
{
protected $_comment;
protected function setUp()
{
$this->_comment = new Application_Model_Comment();
parent::setUp();
}
protected function tearDown()
{
parent::tearDown();
$this->_comment = null;
}
public function testModelIsEmptyAtConstruct()
{
$this->assertSame(0, $this->_comment->getId());
$this->assertNull($this->_comment->getFullName());
$this->assertNull($this->_comment->getEmailAddress());
$this->assertNull($this->_comment->getWebsite());
$this->assertNull($this->_comment->getComment());
}
}
41. WriIng
the
class
<?php
class Application_Model_Comment
{
protected $_id = 0; protected $_fullName; protected $_emailAddress;
protected $_website; protected $_comment;
public function setId($id) { $this->_id = (int) $id; return $this; }
public function getId() { return $this->_id; }
public function setFullName($fullName) { $this->_fullName = (string) $fullName; return $this; }
public function getFullName() { return $this->_fullName; }
public function setEmailAddress($emailAddress) { $this->_emailAddress = (string) $emailAddress; return $this; }
public function getEmailAddress() { return $this->_emailAddress; }
public function setWebsite($website) { $this->_website = (string) $website; return $this; }
public function getWebsite() { return $this->_website; }
public function setComment($comment) { $this->_comment = (string) $comment; return $this; }
public function getComment() { return $this->_comment; }
public function populate($row) {
if (is_array($row)) {
$row = new ArrayObject($row, ArrayObject::ARRAY_AS_PROPS);
}
if (isset ($row->id)) $this->setId($row->id);
if (isset ($row->fullName)) $this->setFullName($row->fullName);
if (isset ($row->emailAddress)) $this->setEmailAddress($row->emailAddress);
if (isset ($row->website)) $this->setWebsite($row->website);
if (isset ($row->comment)) $this->setComment($row->comment);
}
public function toArray() {
return array (
'id' => $this->getId(),
'fullName' => $this->getFullName(),
'emailAddress' => $this->getEmailAddress(),
'website' => $this->getWebsite(),
'comment' => $this->getComment(),
);
}
}
41
54. AutomaIon
54
• Limit
risk
of
human
error
• Will
always
be
executed
in
same
order
• Can
be
shared
amongst
team
members
• Allows
to
fine-‐tune
and
improve
features