symbiote / silverstripe-build

Phing build settings and tasks for the silverstripe-base project
BSD 3-Clause "New" or "Revised" License
0 stars 8 forks source link

Example of integrating PHPStan #25

Closed silbinarywolf closed 6 years ago

silbinarywolf commented 6 years ago

Example use of calling in CLI (when developing on your local): phing phpstan

Add to phing ci_test target: (to execute in CI)

<!-- Override ci_test from build/buildfile.xml to include phpstan -->
<target name="ci_test" depends="build">
    <property name="flush" value="1" override="true" />
    <property name="build" value="1" override="true" />
    <phingcall target="phpstan" />
    <phingcall target="test" />
</target>

Mirror:

<property name="phpstan.dir" value="mysite/code my-internal-module/code" />
<target name="phpstan" depends="get-composer">
    <!-- 
        Example CLI use: `phing phpstan -Dphpstan.level=2`
    -->
    <if>
        <available file="${project.basedir}/vendor/bin/phpstan" />
        <then>
            <property name="phpstan.installed" value="1" />
        </then>
    </if>
    <if>
        <available file="${project.basedir}/vendor/silbinarywolf/silverstripe-phpstan" type="dir" />
        <then>
            <property name="phpstan.silverstripe_installed" value="1" />
        </then>
    </if>
    <if>
        <isset property="phpstan.debug"/>
        <then>
            <property name="phpstan.debug_cmd" value="--debug" />
        </then>
        <else>
            <property name="phpstan.debug_cmd" value="" />
        </else>
    </if>

    <if>
        <not><isset property="phpstan.disabled"/></not>
        <then>
            <!-- Run PHPStan if installed -->
            <if>
                <isset property="phpstan.installed"/>
                <then>
                    <if>
                        <isset property="phpstan.silverstripe_installed"/>
                        <then>
                            <!-- Default values -->
                            <if>
                                <not><isset property="phpstan.level" /></not>
                                <then>
                                    <property name="phpstan.level" value="2" />
                                </then>
                            </if>
                            <if>
                                <not><isset property="phpstan.dir" /></not>
                                <then>
                                    <property name="phpstan.dir" value="mysite" />
                                </then>
                            </if>
                            <!-- Run PHPStan -->
                            <exec executable="vendor/bin/phpstan" 
                                  passthru="true" 
                                  checkreturn="true"
                            >
                                <arg value="analyse" />
                                <arg line="${phpstan.dir}" />
                                <arg value="-c" />
                                <arg value="phpstan.neon" />
                                <arg value="-a" />
                                <arg value="vendor/silbinarywolf/silverstripe-phpstan/bootstrap.php" />
                                <arg value="--level"/>
                                <arg value="${phpstan.level}"/>
                                <arg line="${phpstan.debug_cmd}"/>
                            </exec>
                        </then>
                        <else>
                            <echo msg="PHPStan SilverStripe extension is not installed in this project and was not executed." />
                        </else>
                    </if>
                </then>
                <else>
                    <echo msg="PHPStan is not installed in this project and was not executed." />
                </else>
            </if>
        </then>
        <else>
            <echo msg="PHPStan has been disabled via the 'phpstan.disabled' property." />
        </else>
    </if>
</target>
nglasl commented 6 years ago

@silbinarywolf, can you please confirm whether this is intended for SS3 and/or SS4?

silbinarywolf commented 6 years ago

@nglasl It'll work with both.

silbinarywolf commented 6 years ago

@nglasl This is done and in master. https://github.com/symbiote/silverstripe-build/blob/a227dc8c490a27a2782af974098c50804ba248aa/buildfile.xml#L566

Raised a seperate issue for handling backporting to SS3 (you might never need this) https://github.com/symbiote/silverstripe-build/issues/31