Search code examples
jenkinszend-framework2phplint

Build and deploy a Zend Framework PHP application in Jenkins


I'm setting up a jenkins job to build and deploy a Zend Framework 2 php application. In my ant build script I've defined a lint job for validating php files.

The build job failed because lint detected an error in a ZF2 library file.

This is the output generated by lint:

[apply] PHP Fatal error:  Constructor Zend\Captcha\Factory::factory() cannot be static in /var/lib/jenkins/workspace/XXX/vendor/zendframework/zendframework/library/Zend/Captcha/Factory.php on line 90
[apply] Errors parsing /var/lib/jenkins/workspace/XXX/vendor/zendframework/zendframework/library/Zend/Captcha/Factory.php

Does anybody know why the validation of Zend/Captcha/Factory.php fails ?

The ANT Task looks like this:

    <target name="lint" description="Perform syntax check of sourcecode files">
  <apply executable="php" failonerror="true">
   <arg value="-l" />

   <fileset dir="${basedir}/">
    <include name="**/*.php" />
    <modified />
   </fileset>

   <fileset dir="${basedir}/tests">
    <include name="**/*.php" />
    <modified />
   </fileset>
  </apply>
 </target>

Solution

  • Your problem is because Zend Framework requires php 5.3.3 or later. Since your Jenkins box uses 5.3.2, this give a variety of problems. One of them is apparently the error you have now.

    I think you haven't noticed the error before because on the development system you have a 5.3.3+ install. Try to update your testing environment to a newer version of php, that will remove this particular problem.

    Update

    To clarify my answer a bit, there is one backwards compatibility break in php 5.3.3 which comes back in your environment. Check this changelog and particularly this statement:

    Backwards incompatible change:

    Methods with the same name as the last element of a namespaced class name will no longer be treated as constructor. This change doesn't affect non-namespaced classes.

    <?php
    namespace Foo;
    class Bar {
        public function Bar() {
            // treated as constructor in PHP 5.3.0-5.3.2
            // treated as regular method in PHP 5.3.3
        }
    }
    ?>
    

    There is no impact on migration from 5.2.x because namespaces were only introduced in PHP 5.3.

    In the case of Zend\Captcha\Factory, there is a method factory() which is static so you can call Zend\Captcha\Factory::factory(). On php 4 and 5 up to 5.3.2, this method is also parsed as the constructor for the factory. And constructors cannot be static.

    A linter will give you a fatal error for this case.