Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to allow having an undercore in test methods #257

Merged
merged 1 commit into from Sep 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/main/php/PHPMD/Rule/Controversial/CamelCaseMethodName.php
Expand Up @@ -84,14 +84,7 @@ public function apply(AbstractNode $node)
{
$methodName = $node->getName();
if (!in_array($methodName, $this->ignoredMethods)) {
$pattern = '/^[a-z][a-zA-Z0-9]*$/';
$allowUnderscore = $this->getBooleanProperty('allow-underscore');

if ($allowUnderscore == true) {
$pattern = '/^[_]?[a-z][a-zA-Z0-9]*$/';
}

if (!preg_match($pattern, $methodName)) {
if (!$this->isValid($methodName)) {
$this->addViolation(
$node,
array(
Expand All @@ -101,4 +94,18 @@ public function apply(AbstractNode $node)
}
}
}

private function isValid($methodName)
{
if ($this->getBooleanProperty('allow-underscore-test') && strpos($methodName, 'test') === 0) {
return preg_match('/^test[a-zA-Z0-9]*([_][a-z][a-zA-Z0-9]*)?$/', $methodName);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified to only allow:
3 or more words (including "test") before the _
2 or more words after the _
or 1 or more words but no underscore
A word starts with an uper case A-Z, then zero or more lower case a-z, then zero or more numbers 0-9 are allowed.
/^test(([A-Z][a-z][0-9])+|([A-Z][a-z][0-9]){3,}_{2,}))$/
Tests not included. Wielding this regexp is unsafe without first properly testing it.

}

if ($this->getBooleanProperty('allow-underscore')) {
return preg_match('/^[_]?[a-z][a-zA-Z0-9]*$/', $methodName);
}

return preg_match('/^[a-z][a-zA-Z0-9]*$/', $methodName);
}

}
230 changes: 115 additions & 115 deletions src/site/rst/documentation/ant-task.rst
@@ -1,115 +1,115 @@
====================
PHPMD ant task usage
====================
To ease the usage of PHPMD in your build process we provide an `ant task`__
that integrates PHPMD into the `ant`__ build tool. The ant task jar file can
be found in the `download`__ section of the PHPMD homepage.
__ http://ant.apache.org/manual/targets.html
__ http://ant.apache.org/
__ /download/extensions#ant-task
To make the task available in your ant build file you have two options.
The first option is to copy or link the ``*.jar`` file into the ``lib``
directory of your ant installation. ::
mapi@arwen ~ $ wget \
http://phpmd.org/download/extensions/ant-phpmd-0.1.0.jar
...
mapi@arwen ~ $ ln -s ~/ant-phpmd-0.1.0.jar /opt/ant/lib/ant-phpmd.jar
The second option is to call ant with the command line switch ``-lib`` ::
mapi@arwen ~ $ ant -lib ant-phpmd-0.1.0.jar
Now we can start using PHPMD in our ant ``build.xml`` file by adding
a task definition that informs ant about the new task and registers
it with a custom identifier. ::
<?xml version="1.0" encoding="UTF-8" ?>
<project name="phpmd.example" default="example" basedir=".">
<taskdef name="phpmd" classname="org.phpmd.ant.PHPMDTask"/>
</project>
Now the PHPMD task can be called through an xml element named ``<phpmd />``
in this build: ::
<?xml version="1.0" encoding="UTF-8"?>
<project name="phpmd.example" default="example" basedir=".">
<taskdef name="phpmd" classname="org.phpmd.ant.PHPMDTask"/>
<target name="example">
<phpmd rulesetfiles="unusedcode" failonerror="off">
<formatter type="xml" toFile="${basedir}/pmd.xml" />
<fileset dir="${basedir}/PHPMD">
<include name="*.php" />
</fileset>
</phpmd>
</target>
</project>
Parameters
==========
The following attributes can be defined on the PHPMD task xml-element.
===================== ========================================================================================================================= ================================================
Attribute Description Required
===================== ========================================================================================================================= ================================================
rulesetfiles A comma delimited list of ruleset files ('rulesets/basic.xml,rulesets/design.xml') or identifiers of build-in rulesets. Yes, unless the ruleset nested element is used
failonerror Whether or not to fail the build if any errors occur while processing the files No
failOnRuleViolation Whether or not to fail the build if PHPMD finds any problems No
minimumPriority The rule priority threshold; rules with lower priority than they will not be used No
===================== ========================================================================================================================= ================================================
Nested xml elements
===================
The ``<formatter />`` specifies the format of and the output file to
which the report is written.
**Parameters**
=========== =================================================================== ==========
Attribute Description Required
=========== =================================================================== ==========
format The report output format. Supported formats are xml,html and text. Yes
toFile A filename into which the report is written Yes
=========== =================================================================== ==========
The ``<ruleset />`` xml element is another way to specify rulesets. Here
is a modified version of the previous example: ::
<target name="example">
<phpmd failonerror="off">
<formatter type="text" toFile="${basedir}/pmd.xml" />
<ruleset>unusedcode</ruleset>
<ruleset>codesize</ruleset
<fileset dir="${basedir}/PHPMD">
<include name="*.php" />
</fileset>
</phpmd>
</target>
Postprocessing the report file with XSLT
========================================
There are several XSLT scripts which can be used to transform the XML
report into some nice html pages. To do this, make sure you use the
XML formatter in the PHPMD task invocation, i.e.: ::
<formatter type="xml" toFile="${builddir}/~report.xml"/>
Then, after the end of the PHPMD task, do this: ::
<xslt in="${builddir}/~report.xml"
style="${basedir}/report.xslt"
out="${reportdir}/report.html" />
====================
PHPMD ant task usage
====================

To ease the usage of PHPMD in your build process we provide an `ant task`__
that integrates PHPMD into the `ant`__ build tool. The ant task jar file can
be found in the `download`__ section of the PHPMD homepage.

__ http://ant.apache.org/manual/targets.html
__ http://ant.apache.org/
__ /download/extensions#ant-task

To make the task available in your ant build file you have two options.
The first option is to copy or link the ``*.jar`` file into the ``lib``
directory of your ant installation. ::

mapi@arwen ~ $ wget \
http://phpmd.org/download/extensions/ant-phpmd-0.1.0.jar
...
mapi@arwen ~ $ ln -s ~/ant-phpmd-0.1.0.jar /opt/ant/lib/ant-phpmd.jar

The second option is to call ant with the command line switch ``-lib`` ::

mapi@arwen ~ $ ant -lib ant-phpmd-0.1.0.jar


Now we can start using PHPMD in our ant ``build.xml`` file by adding
a task definition that informs ant about the new task and registers
it with a custom identifier. ::

<?xml version="1.0" encoding="UTF-8" ?>
<project name="phpmd.example" default="example" basedir=".">

<taskdef name="phpmd" classname="org.phpmd.ant.PHPMDTask"/>

</project>

Now the PHPMD task can be called through an xml element named ``<phpmd />``
in this build: ::

<?xml version="1.0" encoding="UTF-8"?>
<project name="phpmd.example" default="example" basedir=".">

<taskdef name="phpmd" classname="org.phpmd.ant.PHPMDTask"/>

<target name="example">
<phpmd rulesetfiles="unusedcode" failonerror="off">
<formatter type="xml" toFile="${basedir}/pmd.xml" />
<fileset dir="${basedir}/PHPMD">
<include name="*.php" />
</fileset>
</phpmd>
</target>

</project>

Parameters
==========

The following attributes can be defined on the PHPMD task xml-element.

===================== ========================================================================================================================= ================================================
Attribute Description Required
===================== ========================================================================================================================= ================================================
rulesetfiles A comma delimited list of ruleset files ('rulesets/basic.xml,rulesets/design.xml') or identifiers of build-in rulesets. Yes, unless the ruleset nested element is used
failonerror Whether or not to fail the build if any errors occur while processing the files No
failOnRuleViolation Whether or not to fail the build if PHPMD finds any problems No
minimumPriority The rule priority threshold; rules with lower priority than they will not be used No
===================== ========================================================================================================================= ================================================

Nested xml elements
===================

The ``<formatter />`` specifies the format of and the output file to
which the report is written.

**Parameters**

=========== =================================================================== ==========
Attribute Description Required
=========== =================================================================== ==========
format The report output format. Supported formats are xml,html and text. Yes
toFile A filename into which the report is written Yes
=========== =================================================================== ==========

The ``<ruleset />`` xml element is another way to specify rulesets. Here
is a modified version of the previous example: ::

<target name="example">
<phpmd failonerror="off">
<formatter type="text" toFile="${basedir}/pmd.xml" />
<ruleset>unusedcode</ruleset>
<ruleset>codesize</ruleset
<fileset dir="${basedir}/PHPMD">
<include name="*.php" />
</fileset>
</phpmd>
</target>

Postprocessing the report file with XSLT
========================================

There are several XSLT scripts which can be used to transform the XML
report into some nice html pages. To do this, make sure you use the
XML formatter in the PHPMD task invocation, i.e.: ::

<formatter type="xml" toFile="${builddir}/~report.xml"/>

Then, after the end of the PHPMD task, do this: ::

<xslt in="${builddir}/~report.xml"
style="${basedir}/report.xslt"
out="${reportdir}/report.html" />


77 changes: 77 additions & 0 deletions src/test/php/PHPMD/Rule/Controversial/CamelCaseMethodNameTest.php
Expand Up @@ -72,6 +72,7 @@ public function testRuleDoesNotApplyForValidMethodName()
$rule = new CamelCaseMethodName();
$rule->setReport($report);
$rule->addProperty('allow-underscore', 'false');
$rule->addProperty('allow-underscore-test', 'false');
$rule->apply($this->getMethod());
}

Expand All @@ -90,6 +91,7 @@ public function testRuleDoesApplyForMethodNameWithCapital()
$rule = new CamelCaseMethodName();
$rule->setReport($report);
$rule->addProperty('allow-underscore', 'false');
$rule->addProperty('allow-underscore-test', 'false');
$rule->apply($method);
}

Expand All @@ -108,6 +110,7 @@ public function testRuleDoesApplyForMethodNameWithUnderscores()
$rule = new CamelCaseMethodName();
$rule->setReport($report);
$rule->addProperty('allow-underscore', 'false');
$rule->addProperty('allow-underscore-test', 'false');
$rule->apply($method);
}

Expand All @@ -125,6 +128,7 @@ public function testRuleDoesApplyForValidMethodNameWithUnderscoreWhenNotAllowed(
$rule = new CamelCaseMethodName();
$rule->setReport($report);
$rule->addProperty('allow-underscore', 'false');
$rule->addProperty('allow-underscore-test', 'false');
$rule->apply($method);
}

Expand All @@ -142,6 +146,79 @@ public function testRuleDoesNotApplyForValidMethodNameWithUnderscoreWhenAllowed(
$rule = new CamelCaseMethodName();
$rule->setReport($report);
$rule->addProperty('allow-underscore', 'true');
$rule->addProperty('allow-underscore-test', 'false');
$rule->apply($method);
}

/**
* Tests that the rule does apply for a valid test method name
* with an underscore.
*
* @return void
*/
public function testRuleDoesApplyForTestMethodWithUnderscoreWhenNotAllowed()
{
$method = $this->getMethod();
$report = $this->getReportMock(1);

$rule = new CamelCaseMethodName();
$rule->setReport($report);
$rule->addProperty('allow-underscore', 'false');
$rule->addProperty('allow-underscore-test', 'false');
$rule->apply($method);
}

/**
* Tests that the rule does not apply for a valid test method name
* with an underscore when an single underscore is allowed.
*
* @return void
*/
public function testRuleDoesNotApplyForTestMethodWithUnderscoreWhenAllowed()
{
$method = $this->getMethod();
$report = $this->getReportMock(0);

$rule = new CamelCaseMethodName();
$rule->setReport($report);
$rule->addProperty('allow-underscore', 'false');
$rule->addProperty('allow-underscore-test', 'true');
$rule->apply($method);
}

/**
* Tests that the rule does apply for a test method name
* with multiple underscores even when one is allowed.
*
* @return void
*/
public function testRuleAppliesToTestMethodWithTwoUnderscoresEvenWhenOneIsAllowed()
{
$method = $this->getMethod();
$report = $this->getReportMock(1);

$rule = new CamelCaseMethodName();
$rule->setReport($report);
$rule->addProperty('allow-underscore', 'false');
$rule->addProperty('allow-underscore-test', 'true');
$rule->apply($method);
}

/**
* Tests that the rule does apply to for test method names that
* have a capital after their single allowed underscore.
*
* @return void
*/
public function testRuleAppliesToTestMethodWithUnderscoreFollowedByCapital()
{
$method = $this->getMethod();
$report = $this->getReportMock(1);

$rule = new CamelCaseMethodName();
$rule->setReport($report);
$rule->addProperty('allow-underscore', 'false');
$rule->addProperty('allow-underscore-test', 'true');
$rule->apply($method);
}

Expand Down
@@ -0,0 +1,8 @@
<?php
class testRuleAppliesToTestMethodWithTwoUnderscoresEvenWhenOneIsAllowed
{
public function testGivenSomeValue_expectSome_niceResult()
{

}
}
@@ -0,0 +1,8 @@
<?php
class testRuleAppliesToTestMethodWithUnderscoreFollowedByCapital
{
public function testGivenSomeValue_ExpectSomeResult()
{

}
}