Build That Phing!

Download Build That Phing!

Post on 23-Aug-2014

285 views

Embed Size (px)

DESCRIPTION

PHPMD, PHPCS, PHPUnit and PHPLint; these are all tools to maintain code quality. Executing these tasks is a chore and there are many ways to automate this process, Phing being one of them. The goal of this talk is to really discover what Phing has to offer, from implementing code logic to creating your own task.

TRANSCRIPT

<ul><li> Build that Phing! PHing Is Not GNU make /Rick Kuipers @rskuipers io@rskuipers.com </li> <li> whoami Rick Kuipers @rskuipers Apeldoorn The Webmen Magento Zend Framework </li> <li> &gt;&lt; A build tool developed for and in PHP Installation through PEAR or Composer $ pear channel-discover pear.phing.info $ pear install --alldeps phing/phing </li> <li> &gt;&lt; vs PHP PEAR build.xml Java apt-get build.xml </li> <li> Skeleton Application </li> <li> build.xml </li> <li> $ phing -l Buildfile: /var/www/zf2/build.xml Warning: target 'build' has no tasks or dependencies Default target: ------------------------------------------------------------------------------- build Subtargets: ------------------------------------------------------------------------------- build </li> <li> $ phing Buildfile: /var/www/zf2/build.xml Warning: target 'build' has no tasks or dependencies zf2 tutorial &gt; build: BUILD FINISHED Total time: 0.0457 seconds </li> <li> Fundamentals Properties FileSets PatternSets Tasks </li> <li> Properties </li> <li> FileSets . |-- build.xml |-- example.php |-- phing-logic-1 | `-- build.xml |-- phing-logic-2 | |-- build.xml | `-- continue |-- phing-logic-foreach | |-- build.xml | `-- fruits | |-- apple.php | |-- banana.xml | |-- orange.xml | `-- pear.php `-- qa-basics |-- build.xml |-- example.php `-- tests `-- ExampleTest.php </li> <li> . |-- build.xml |-- example.php |-- phing-logic-1 | `-- build.xml |-- phing-logic-2 | |-- build.xml | `-- continue |-- phing-logic-foreach | |-- build.xml | `-- fruits | |-- apple.php | |-- banana.xml | |-- orange.xml | `-- pear.php `-- qa-basics |-- build.xml |-- example.php `-- tests `-- ExampleTest.php </li> <li> . |-- build.xml |-- example.php |-- phing-logic-1 | `-- build.xml |-- phing-logic-2 | |-- build.xml | `-- continue |-- phing-logic-foreach | |-- build.xml | `-- fruits | |-- apple.php | |-- banana.xml | |-- orange.xml | `-- pear.php `-- qa-basics |-- build.xml |-- example.php `-- tests `-- ExampleTest.php </li> <li> PatternSets </li> <li> Let's put it together! </li> <li> $ phing Buildfile: /var/www/zf2/build.xml Warning: target 'build' has no tasks or dependencies zf2 &gt; build: BUILD FINISHED Total time: 0.0492 seconds </li> <li> How do we use Phing QA during module development Combination with CI </li> <li> Quality Assurance "Quality Assurance (QA) is a way of preventing mistakes or defects in manufactured products and avoiding problems when delivering solutions or services to customers." </li> <li> QA Tools PHP Lint PHP Mess Detector PHP Code Sniffer PHP Unit </li> <li> Example 1. </li> <li> PHP Lint Built into the PHP executable. Checks for syntax errors. $ php -l example.php PHP Parse error: syntax error, unexpected 'class' (T_CLASS), expecting (T_NS_SEPARATOR) o r ';' or '{' in example.php on line 5 Errors parsing example.php </li> <li> Example 1. </li> <li> PHP Mess Detector Installation through Git, Composer or as a PEAR package. Detects code smells such as: Possible bugs Suboptimal code Overcomplicated expressions Unused parameters, methods, properties $ phpmd example.php text cleancode,codesize,controversial,design,naming,unusedcode example.php:7 Avoid variables with short names like $x. Configured minimum length is 3. example.php:7 Avoid variables with short names like $y. Configured minimum length is 3. example.php:7 Avoid variables with short names like $z. Configured minimum length is 3. example.php:7 Avoid unused parameters such as '$z'. </li> <li> Example 1. </li> <li> PHP Code Sniffer Installation through Git, Composer or as a PEAR package. Checks your code for coding standards such as: PEAR PSR1 PSR2 Zend Squiz $ phpcs --standard=PSR2 example.php -------------------------------------------------------------------------------- FOUND 4 ERROR(S) AFFECTING 4 LINE(S) -------------------------------------------------------------------------------- 5 | ERROR | Opening brace of a class must be on the line after the definition 7 | ERROR | Visibility must be declared on method "sum" 12 | ERROR | The closing brace for the class must go on the next line after | | the body 13 | ERROR | A closing tag is not permitted at the end of a PHP file -------------------------------------------------------------------------------- </li> <li> Example 1. </li> <li> PHP Unit Installation through PHAR, Composer or as a PEAR package. Runs tests on your code. Provides a framework to create tests. </li> <li> $ phing Buildfile: /var/www/zf2/build.xml Warning: target 'build' has no tasks or dependencies zf2 &gt; build: BUILD FINISHED Total time: 0.0540 seconds </li> <li> Dependencies </li> <li> $ phing Buildfile: /var/www/zf2/build.xml zf2 &gt; phplint: BUILD FAILED exception 'IOException' with message 'Unable to open /var/www/zf2/build/cache/lint.cache for writing: ' in /usr/share/php/phing/system/io/FileOutputStream.php:59 Stack trace: #0 /usr/share/php/phing/system/io/FileWriter.php(38): FileOutputStream-&gt;__construct(Object(Ph ingFile), false) #1 /usr/share/php/phing/util/DataStore.php(146): FileWriter-&gt;__construct(Object(PhingFile)) #2 /usr/share/php/phing/util/DataStore.php(107): DataStore-&gt;write() #3 /usr/share/php/phing/tasks/ext/PhpLintTask.php(198): DataStore-&gt;commit() #4 /usr/share/php/phing/UnknownElement.php(96): PhpLintTask-&gt;main() #5 /usr/share/php/phing/Task.php(260): UnknownElement-&gt;main() #6 /usr/share/php/phing/Target.php(297): Task-&gt;perform() #7 /usr/share/php/phing/Target.php(320): Target-&gt;main() #8 /usr/share/php/phing/Project.php(824): Target-&gt;performTasks() #9 /usr/share/php/phing/Project.php(797): Project-&gt;executeTarget('build') #10 /usr/share/php/phing/Phing.php(586): Project-&gt;executeTargets(Array) #11 /usr/share/php/phing/Phing.php(170): Phing-&gt;runBuild() #12 /usr/share/php/phing/Phing.php(278): Phing::start(Array, NULL) #13 /usr/share/php/phing.php(43): Phing::fire(Array) #14 {main} Total time: 0.1397 seconds </li> <li> Stephan Hochdrfer </li> <li> $ phing Buildfile: /var/www/zf2/build.xml zf2 &gt; -prepare: zf2 &gt; phplint: zf2 &gt; phpcs: FILE: /var/www/zf2/module/Application/view/layout/layout.phtml -------------------------------------------------------------------------------- FOUND 0 ERROR(S) AND 5 WARNING(S) AFFECTING 5 LINE(S) -------------------------------------------------------------------------------- 6 | WARNING | Line exceeds 120 characters; contains 127 characters 11 | WARNING | Line exceeds 120 characters; contains 154 characters 17 | WARNING | Line exceeds 120 characters; contains 137 characters 33 | WARNING | Line exceeds 120 characters; contains 125 characters 36 | WARNING | Line exceeds 120 characters; contains 123 characters -------------------------------------------------------------------------------- </li> <li> zf2 &gt; phpmd: [phpmd] Processing files... /var/www/zf2/module/Album/src/Album/Form/AlbumForm.php:8 Avoid unused parameters such as '$ name'. /var/www/zf2/module/Album/src/Album/Model/Album.php:33 Avoid unused parameters such as '$ inputFilter'. [phpmd] Finished processing files zf2 &gt; phpunit: PHPUnit 4.0.14 by Sebastian Bergmann. Configuration read from /var/www/zf2/module/Application/test/phpunit.xml.dist . Time: 42 ms, Memory: 5.75Mb OK (1 test, 1 assertion) PHPUnit 4.0.14 by Sebastian Bergmann. Configuration read from /var/www/zf2/module/Album/test/phpunit.xml.dist ............. Time: 72 ms, Memory: 8.50Mb OK (13 tests, 22 assertions) zf2 &gt; build: BUILD FINISHED </li> <li> Logical tasks IfTask ConditionTask FailTask ForeachTask </li> <li> IfTask </li> <li> ConditionTask $ phing Buildfile: /var/www/talk-phing/web/examples/phing-condition-task/build.xml Example &gt; build: [echo] It's recursive! BUILD FINISHED Total time: 0.0487 seconds </li> <li> $ phing Buildfile: /var/www/talk-phing/web/examples/phing-condition-task/build.xml Example &gt; build: [echo] It's recursive! BUILD FINISHED Total time: 0.0524 seconds </li> <li> $ phing Buildfile: /var/www/talk-phing/web/examples/phing-condition-task/build.xml Example &gt; build: [echo] It's not recursive! BUILD FINISHED Total time: 0.0468 seconds </li> <li> FailTask </li> <li> ForeachTask </li> <li> Interesting Tasks PropertyPromptTask PhpEvalTask </li> <li> PropertyPromptTask </li> <li> $ phing Buildfile: /var/www/talk-phing/web/examples/phing-prompt/build.xml Please enter your name [anonymous] ? Rick Example &gt; build: [echo] Example: Hello, Rick! BUILD FINISHED Total time: 3.7389 seconds </li> <li> PhpEvalTask 1 </li> <li> $ phing Buildfile: /var/www/talk-phing/web/examples/phing-php-eval/build.xml Example &gt; build: [php] Evaluating PHP expression: function increment($i) { return ++$i; } [php] Calling PHP function: increment() [echo] 2 BUILD FINISHED Total time: 0.0511 seconds </li> <li> phing Expected: ${expected} Result: ${result} </li> <li> $ phing Buildfile: /var/www/talk-phing/web/examples/phing-testing-framework/build.xml Testing Framework &gt; build: [php] Calling PHP function: ucfirst() [phingcall] Calling Buildfile '/var/www/talk-phing/web/examples/phing-testing-framework/build .xml' with target 'test' Testing Framework &gt; test: [echo] [php] Evaluating PHP expression: preg_replace('/php/', 'xml', 'PHP') [phingcall] Calling Buildfile '/var/www/talk-phing/web/examples/phing-testing-framework/build .xml' with target 'test' Testing Framework &gt; test: [echo] [if] Error in IfTask Execution of target "test" failed for the following reason: /var/www/talk-phing/web/examples/ phing-testing-framework/build.xml:19:12: Error in IfTask Previous exception 'BuildException' with message '/var/www/talk-phing/web/examples/phing-test ing-framework/build.xml:26:25: /var/www/talk-phing/web/examples/phing-testing-framework/build .xml:26:25: Expected: xml Result: PHP </li> <li> Extending our phpunit target ${absFile} </li> <li> Create your own Task </li> <li> VDD var_dump driven development XDebug or PHPDBG </li> <li> VarDumpTask Accept a fileset Find lines that contain var_dump Ignore comments Show the files and line numbers that match Halt on match </li> <li> ${absFile} </li> <li> Duplicate build file logic Build file inheritance Git submodules </li> <li> phing/default.xml </li> <li> build.xml </li> <li> Documentation http://www.phing.info/docs/guide/stable/ https://github.com/phingofficial/phing </li> </ul>