Skip to content

Commit

Permalink
Add support for formatter nested tag to the phpcs task (#1614)
Browse files Browse the repository at this point in the history
* Add support for formatter nested tag to the phpcs task

* Fix some Coding Standards

* Seperate PhpCSTaskFormatterElement class out to separate file.

* Minor coding standard change required.

* Minor coding standard change required.

* Fix issue highlighted by phpstan

Co-authored-by: Siad Ardroumli <siad.ardroumli@gmail.com>
  • Loading branch information
kenguest and siad007 authored Jul 1, 2021
1 parent b747fed commit be0e2ae
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 1 deletion.
1 change: 1 addition & 0 deletions etc/phing-grammar.rng
Original file line number Diff line number Diff line change
Expand Up @@ -5312,6 +5312,7 @@
</interleave>
<zeroOrMore>
<ref name="fileset"/>
<ref name="formatter"/>
</zeroOrMore>
</element>
</define>
Expand Down
29 changes: 28 additions & 1 deletion src/Phing/Task/Optional/PhpCSTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
/**
* A PHP code sniffer task. Checking the style of one or more PHP source files.
*
* @author Siad Ardroumli <siad.ardroumli@gmail.com>Ex
* @author Siad Ardroumli <siad.ardroumli@gmail.com>
*/
class PhpCSTask extends Task
{
Expand Down Expand Up @@ -74,6 +74,8 @@ class PhpCSTask extends Task
/** @var string */
private $format = '';

protected $formatters = [];

/** @var string */
private $bin = 'phpcs';

Expand Down Expand Up @@ -115,6 +117,21 @@ public function setFormat(string $format): void
$this->project->log("Format set to $format", Project::MSG_VERBOSE);
}

/**
* Create object for nested formatter element.
* @return CodeSnifferFormatterElement
*/
public function createFormatter()
{
$num = array_push(
$this->formatters,
new PhpCSTaskFormatterElement()
);

return $this->formatters[$num - 1];
}


public function setStandard(string $standard): void
{
$this->standard = $standard;
Expand Down Expand Up @@ -166,6 +183,16 @@ public function main()
$toExecute->createArgument()->setValue(' --report-file=' . $this->outfile);
}

foreach ($this->formatters as $formatter) {
$formatterReportFile = ($formatter->getUseFile() ? $formatter->getOutFile() : null);
$formatterType = $formatter->getType();
$this->project->log(
"Generate report of type \"{$formatterType}\" with report written to $formatterReportFile",
Project::MSG_VERBOSE
);
$toExecute->createArgument()->setValue(' --report-' . $formatterType . '=' . $formatterReportFile);
}

if (null !== $this->file) {
$toExecute->createArgument()->setFile($this->file);
} else {
Expand Down
87 changes: 87 additions & 0 deletions src/Phing/Task/Optional/PhpCSTaskFormatterElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/

namespace Phing\Task\Optional;

use Phing\Exception\BuildException;

class PhpCSTaskFormatterElement extends \Phing\Type\DataType
{
/**
* Type of output to generate
* @var string
*/
protected $type = "";

/**
* Output to file?
* @var bool
*/
protected $useFile = true;

/**
* Output file.
* @var string
*/
protected $outfile = "";

/**
* Validate config.
*/
public function parsingComplete()
{
if (empty($this->type)) {
throw new BuildException("Format missing required 'type' attribute.");
}
if ($this->useFile && empty($this->outfile)) {
throw new BuildException("Format requires 'outfile' attribute when 'useFile' is true.");
}
}

public function setType($type)
{
$this->type = $type;
}

public function getType()
{
return $this->type;
}

public function setUseFile($useFile)
{
$this->useFile = $useFile;
}

public function getUseFile()
{
return $this->useFile;
}

public function setOutfile($outfile)
{
$this->outfile = $outfile;
}

public function getOutfile()
{
return $this->outfile;
}
}
7 changes: 7 additions & 0 deletions tests/Phing/Task/Optional/PhpCSTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,11 @@ public function testFileSetInPhpCs1FormatSet(): void
$this->executeTarget(__FUNCTION__);
$this->assertInLogs('Format set to checkstyle');
}

public function testMultipleReportFormattersSet(): void
{
$this->executeTarget(__FUNCTION__);
$this->assertInLogs('Generate report of type "checkstyle" with report written to /tmp/null1');
$this->assertInLogs('Generate report of type "summary" with report written to /tmp/null2');
}
}
7 changes: 7 additions & 0 deletions tests/etc/tasks/ext/phpcs/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@
<fileset dir="../.." includes="**/*.php"/>
</phpcs>
</target>
<target name="testMultipleReportFormattersSet">
<phpcs bin="../../../../../bin/phpcs" level="debug" checkreturn="false" ignoreAnnotations="true" >
<fileset dir="../.." includes="**/*.php"/>
<formatter type="checkstyle" outfile="/tmp/null1"/>
<formatter type="summary" outfile="/tmp/null2"/>
</phpcs>
</target>
</project>

0 comments on commit be0e2ae

Please sign in to comment.