Quality assurance for php projects with PHPStorm

65
QA for PHP projects using PHPStorm JetBrains Webinar, April 25 2013

description

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.

Transcript of Quality assurance for php projects with PHPStorm

Page 1: Quality assurance for php projects with PHPStorm

QA  for  PHP  projectsusing  PHPStorm

JetBrains  Webinar,  April  25  2013

Page 2: Quality assurance for php projects with PHPStorm

2

Michelangelo  van  Dam

Page 3: Quality assurance for php projects with PHPStorm

Goals

3

• improve  QA  in  your  PHP  projects• deliver  higher  quality  of  applicaIons• become  familiarized  with  available  tools

Page 4: Quality assurance for php projects with PHPStorm

Our  host  for  this  webinar

4

Page 5: Quality assurance for php projects with PHPStorm

Why  Quality  Assurance

5

Page 6: Quality assurance for php projects with PHPStorm

6

Safeguarding  code

Page 7: Quality assurance for php projects with PHPStorm

7

Detect  bugs  early

Page 8: Quality assurance for php projects with PHPStorm

8

Observe  behaviour

Page 9: Quality assurance for php projects with PHPStorm

9

Prevent  accidents

Page 10: Quality assurance for php projects with PHPStorm

10

Tracking  progress

Page 11: Quality assurance for php projects with PHPStorm

Let’s  get  our  hands  dirty

11

Page 12: Quality assurance for php projects with PHPStorm

Revision  Control

12

Page 13: Quality assurance for php projects with PHPStorm

13

FTP

Page 14: Quality assurance for php projects with PHPStorm

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

Page 15: Quality assurance for php projects with PHPStorm

PHP  Lint

15

Page 16: Quality assurance for php projects with PHPStorm

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

Page 17: Quality assurance for php projects with PHPStorm

Running  on  command  line

17

Page 18: Quality assurance for php projects with PHPStorm

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 systemPHP=/usr/bin/phpAWK=/usr/bin/awkGREP=/bin/grepSVNLOOK=/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 fidone

Page 19: Quality assurance for php projects with PHPStorm

No  syntax  failures

19

Page 20: Quality assurance for php projects with PHPStorm

PHPStorm  syntax  checking

20

Page 21: Quality assurance for php projects with PHPStorm

DocumentaIon

21

Page 22: Quality assurance for php projects with PHPStorm

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  ;-­‐)

Page 23: Quality assurance for php projects with PHPStorm

PHPDocumentor2

23

Page 24: Quality assurance for php projects with PHPStorm

Class  details

24

Page 25: Quality assurance for php projects with PHPStorm

Uses  docblock  in  code

25

Page 26: Quality assurance for php projects with PHPStorm

Class  graphs

26

Page 27: Quality assurance for php projects with PHPStorm

Debugging

27

Page 28: Quality assurance for php projects with PHPStorm

The  art  of  finding  a  bug

28

• Debugging  allows  you  to  walk  step-­‐by-­‐step  through  your  code  base  unIl  you  reach  the  point  of  failure.

Page 29: Quality assurance for php projects with PHPStorm

XDebug  php.ini  se`ngs[xdebug]zend_extension=/usr/lib/php/extensions/xdebug.soxdebug.default_enable=1xdebug.cli_color=1xdebug.remote_enable=onxdebug.remote_connect_back=1xdebug.remote_handler=dbgpxdebug.remote_host=127.0.0.1xdebug.remote_port=9000xdebug.scream=1

29

Page 30: Quality assurance for php projects with PHPStorm

Debugging  in  PHPStorm

30

Page 31: Quality assurance for php projects with PHPStorm

TesIng

31

Page 32: Quality assurance for php projects with PHPStorm

Most  common  excuses

32

• no  Ime• not  within  budget• development  team  does  not  know  how• tests  are  provided  aaer  delivery• …

Page 33: Quality assurance for php projects with PHPStorm

NO  EXCUSE!

33

Page 34: Quality assurance for php projects with PHPStorm

Benefits  of  tesIng

34

• beder  code  with  smaller  footprint• allows  refactoring• detects  bugs  in  an  early  stage• saves  Ime  in  maintenance  stage

Page 35: Quality assurance for php projects with PHPStorm

Se`ng  things  up

35

Page 36: Quality assurance for php projects with PHPStorm

ConfiguraIon:  phpunit.xml

36

<phpunit bootstrap="./Bootstrap.php">

<testsuite name="Unit test suite"> <directory>./</directory> </testsuite>

<filter> <whitelist> <directory suffix=".php">../application/</directory> <directory suffix=".php">../library/</directory> </whitelist> </filter>

</phpunit>

Page 37: Quality assurance for php projects with PHPStorm

Bootstrapping  app<?php// set our app paths and environmentsdefine('BASE_PATH', realpath(dirname(__FILE__) . '/../'));define('APPLICATION_PATH', BASE_PATH . '/application');define('TEST_PATH', BASE_PATH . '/tests');define('APPLICATION_ENV', 'testing');

// Include pathset_include_path( . PATH_SEPARATOR . BASE_PATH . '/library' . PATH_SEPARATOR . get_include_path());

// Set the default timezone !!!date_default_timezone_set('Europe/Brussels');

// We wanna catch all errors en strict warningserror_reporting(E_ALL|E_STRICT);

require_once 'Zend/Application.php';$application = new Zend_Application( APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');

$application->bootstrap();

37

Page 38: Quality assurance for php projects with PHPStorm

WriIng  tests

38

Page 39: Quality assurance for php projects with PHPStorm

TesIng  a  class

39

Page 40: Quality assurance for php projects with PHPStorm

WriIng  a  test

40

<?phpclass 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()); }}

Page 41: Quality assurance for php projects with PHPStorm

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

Page 42: Quality assurance for php projects with PHPStorm

Adding  validaIonprotected $_filters;protected $_validators;

public function __construct($params = null){ $this->_filters = array ( 'id' => array ('Int'), 'fullName' => array ('StringTrim', 'StripTags', new Zend_Filter_Alnum(true)), 'emailAddress' => array ('StringTrim', 'StripTags', 'StringToLower'), 'website' => array ('StringTrim', 'StripTags', 'StringToLower'), 'comment' => array ('StringTrim', 'StripTags'), ); $this->_validators = array ( 'id' => array ('Int'), 'fullName' => array ( new Zftest_Validate_Mwop(), new Zend_Validate_StringLength(array ('min' => 4, 'max' => 50)), ), 'emailAddress' => array ( 'EmailAddress', new Zend_Validate_StringLength(array ('min' => 4, 'max' => 50)), ), 'website' => array ( new Zend_Validate_Callback(array('Zend_Uri', 'check')), new Zend_Validate_StringLength(array ('min' => 4, 'max' => 50)), ), 'comment' => array ( new Zftest_Validate_TextBox(), new Zend_Validate_StringLength(array ('max' => 5000)), ), ); if (null !== $params) { $this->populate($params); }}

42

Page 43: Quality assurance for php projects with PHPStorm

Modify  seders  &  gederspublic function setId($id){ $input = new Zend_Filter_Input($this->_filters, $this->_validators); $input->setData(array ('id' => $id)); if (!$input->isValid('id')) { throw new Zend_Exception('Invalid ID provided'); } $this->_id = (int) $input->id; return $this;}

public function setFullName($fullName){ $input = new Zend_Filter_Input($this->_filters, $this->_validators); $input->setData(array ('fullName' => $fullName)); if (!$input->isValid('fullName')) { throw new Zend_Exception('Invalid fullName provided'); } $this->_fullName = (string) $input->fullName; return $this;}

43

Page 44: Quality assurance for php projects with PHPStorm

Modify  seders  &  geders  2public function setEmailAddress($emailAddress){ $input = new Zend_Filter_Input($this->_filters, $this->_validators); $input->setData(array ('emailAddress' => $emailAddress)); if (!$input->isValid('emailAddress')) { throw new Zend_Exception('Invalid emailAddress provided'); } $this->_emailAddress = (string) $input->emailAddress; return $this;}

public function setWebsite($website){ $input = new Zend_Filter_Input($this->_filters, $this->_validators); $input->setData(array ('website' => $website)); if (!$input->isValid('website')) { throw new Zend_Exception('Invalid website provided'); } $this->_website = (string) $input->website; return $this;}

44

Page 45: Quality assurance for php projects with PHPStorm

Modify  geders  &  seders  3public function setComment($comment){ $input = new Zend_Filter_Input($this->_filters, $this->_validators); $input->setData(array ('comment' => $comment)); if (!$input->isValid('comment')) { throw new Zend_Exception('Invalid comment provided'); } $this->_comment = (string) $input->comment; return $this;}

45

Page 46: Quality assurance for php projects with PHPStorm

Running  tests

46

Page 47: Quality assurance for php projects with PHPStorm

Or  in  PHPStorm

47

Page 48: Quality assurance for php projects with PHPStorm

Mess  DetecIon

48

Page 49: Quality assurance for php projects with PHPStorm

Mess  DetecIon

49

• code  smells• possible  bugs• sub-­‐opImal  code• over  complicated  expressions• unused  parameters,  methods  and  properIes• wrongly  named  parameters,  methods  or  properIes

Page 50: Quality assurance for php projects with PHPStorm

Running  on  command  line

50

Page 51: Quality assurance for php projects with PHPStorm

Running  in  PHPStorm

51

Page 52: Quality assurance for php projects with PHPStorm

AutomaIon

52

Page 53: Quality assurance for php projects with PHPStorm

Key  reason

53

“computers are great at doing repetitive tasks very well”

Page 54: Quality assurance for php projects with PHPStorm

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

Page 55: Quality assurance for php projects with PHPStorm

55

AutomaIon  with  Phing

Page 56: Quality assurance for php projects with PHPStorm

Running  Phing  on  CLI

56

Page 57: Quality assurance for php projects with PHPStorm

Running  in  PHPStorm

57

Page 58: Quality assurance for php projects with PHPStorm

Summary

58

Page 59: Quality assurance for php projects with PHPStorm

Overview  of  tools

59

• SCM  (SVN,  GIT,  …)• PHP  Lint• PHP  Document  Generator• Debugging• PHPUnit  TesIng• PHP  Mess  detecIon• Phing  automaIon

}

Page 60: Quality assurance for php projects with PHPStorm

Quality  Assurance

• is  part  of  development  process• will  only  work  if  the  tools  are  available• and  developers  see  the  benefits  of  QA

60

Page 61: Quality assurance for php projects with PHPStorm

PHPStorm  IDE

61

Page 62: Quality assurance for php projects with PHPStorm

Contact

62

Michelangelo van DamZend Certified Engineer

email: [email protected]: michelangelovandamtwitter: @DragonBe

tel EU: +32 15 34 52 90tel US: 202 559-7401

www.in2it.be

facebook.com/in2itvof | @in2itvof

Contact us forConsultancy - Training - QA - Webdesign

Page 63: Quality assurance for php projects with PHPStorm

Credits

I’d like to thank the following people for sharing their creative commons picturesmichelangelo: http://www.flickr.com/photos/dasprid/5148937451birds: http://www.flickr.com/photos/andyofne/4633356197safeguarding: http://www.flickr.com/photos/infidelic/4306205887/bugs: http://www.flickr.com/photos/goingslo/4523034319behaviour: http://www.flickr.com/photos/yuan2003/1812881370prevention: http://www.flickr.com/photos/robertelyov/5159801170progress: http://www.flickr.com/photos/dingatx/4115844000file cabinet: http://www.flickr.com/photos/manc/1427691715documentation: http://www.flickr.com/photos/dennis_matheson/3269442687exam: http://www.flickr.com/photos/albertogp123/5843577306dead roach: http://www.flickr.com/photos/stevensnodgrass/7504408776garbage: http://www.flickr.com/photos/amstersam/4608512202gears: http://www.flickr.com/photos/freefoto/5982549938elephpant: http://www.flickr.com/photos/drewm/3191872515

Page 64: Quality assurance for php projects with PHPStorm

Fork  this  code

http://github.com/DragonBe/zftest

64

Page 65: Quality assurance for php projects with PHPStorm

Thank  you

65