Skip to content

Commit

Permalink
[phploc] Fixed broken task for PHP >= 7.3 (#1387)
Browse files Browse the repository at this point in the history
  • Loading branch information
siad007 authored Sep 3, 2020
1 parent 2ef28b4 commit 8a6a501
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 20 deletions.
6 changes: 3 additions & 3 deletions classes/phing/tasks/ext/phploc/PHPLocFormatterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ public static function createFormatter($formatterElement)

switch ($type) {
case "xml":
include_once 'phing/tasks/ext/phploc/PHPLocXMLFormatter.php';
$formatter = new PHPLocXMLFormatter();
break;
case "json":
$formatter = new PHPLocJSONFormatter();
break;
case "csv":
include_once 'phing/tasks/ext/phploc/PHPLocCSVFormatter.php';
$formatter = new PHPLocCSVFormatter();
break;
case "txt":
case "cli":
include_once 'phing/tasks/ext/phploc/PHPLocTextFormatter.php';
$formatter = new PHPLocTextFormatter();
break;
default:
Expand Down
35 changes: 35 additions & 0 deletions classes/phing/tasks/ext/phploc/PHPLocJSONFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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>.
*/

/**
* @author Siad Ardroumli <siad.ardroumli@gmail.com>
* @package phing.tasks.ext.phploc
*/
class PHPLocJSONFormatter extends AbstractPHPLocFormatter
{
public function printResult(array $count, $countTests = false)
{
if (class_exists('\\SebastianBergmann\\PHPLOC\\Log\\Json')) {
$printer = new SebastianBergmann\PHPLOC\Log\Json();
} else {
throw new BuildException('Not supported PHPLOC version used.');
}
$printer->printResult($this->getToDir() . DIRECTORY_SEPARATOR . $this->getOutfile(), $count);
}
}
6 changes: 3 additions & 3 deletions classes/phing/tasks/ext/phploc/PHPLocTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class PHPLocTask extends Task
/**
* @var array
*/
protected $acceptedReportTypes = ['cli', 'txt', 'xml', 'csv'];
protected $acceptedReportTypes = ['cli', 'txt', 'xml', 'csv', 'json'];

/**
* @var null
Expand Down Expand Up @@ -302,10 +302,10 @@ protected function getFilesToCheck()

if (count($this->filesToCheck) > 0) {
foreach ($this->filesToCheck as $file) {
$files[] = new SplFileInfo($file);
$files[] = (new SplFileInfo($file))->getRealPath();
}
} elseif ($this->fileToCheck !== null) {
$files = [new SplFileInfo($this->fileToCheck)];
$files = [(new SplFileInfo($this->fileToCheck))->getRealPath()];
}

return $files;
Expand Down
16 changes: 7 additions & 9 deletions classes/phing/tasks/ext/phploc/PHPLocTextFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@ class PHPLocTextFormatter extends AbstractPHPLocFormatter
{
public function printResult(array $count, $countTests = false)
{
$printerClass = '\\SebastianBergmann\\PHPLOC\\Log\\Text';
$printer = new $printerClass();
if ($this->getUseFile()) {
$outputClass = '\\Symfony\\Component\\Console\\Output\\StreamOutput';
$stream = fopen($this->getToDir() . DIRECTORY_SEPARATOR . $this->getOutfile(), 'a+');
$output = new $outputClass($stream);
ob_start();
$printer->printResult($count, $countTests);
$output = ob_get_clean();
file_put_contents($this->getToDir() . DIRECTORY_SEPARATOR . $this->getOutfile(), $output, FILE_APPEND);
} else {
$outputClass = '\\Symfony\\Component\\Console\\Output\\ConsoleOutput';
$output = new $outputClass();
$printer->printResult($count, $countTests);
}

$printerClass = '\\SebastianBergmann\\PHPLOC\\Log\\Text';
$printer = new $printerClass();
$printer->printResult($output, $count, $countTests);
}
}
4 changes: 1 addition & 3 deletions classes/phing/tasks/ext/phploc/PHPLocXMLFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ class PHPLocXMLFormatter extends AbstractPHPLocFormatter
{
public function printResult(array $count, $countTests = false)
{
if (class_exists('\\SebastianBergmann\\PHPLOC\\Log\\XML')) {
$printer = new SebastianBergmann\PHPLOC\Log\XML();
} elseif (class_exists('\\SebastianBergmann\\PHPLOC\\Log\\Xml')) {
if (class_exists('\\SebastianBergmann\\PHPLOC\\Log\\Xml')) {
$printer = new SebastianBergmann\PHPLOC\Log\Xml();
} else {
throw new BuildException('Not supported PHPLOC version used.');
Expand Down
11 changes: 9 additions & 2 deletions test/classes/phing/tasks/ext/PHPLOCTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
*
* @author Michiel Rook <mrook@php.net>
* @package phing.tasks.ext
*
* @requires PHP < 7.3
*/
class PHPLOCTaskTest extends BuildFileTest
{
Expand Down Expand Up @@ -59,6 +57,15 @@ public function testReportXML()
unlink(PHING_TEST_BASE . '/etc/tasks/ext/phploc/phploc-report.xml');
}

public function testReportJSON()
{
$this->executeTarget(__FUNCTION__);
$this->assertFileExists(
PHING_TEST_BASE . '/etc/tasks/ext/phploc/phploc-report.json'
);
unlink(PHING_TEST_BASE . '/etc/tasks/ext/phploc/phploc-report.json');
}

public function testFormatters()
{
$this->executeTarget(__FUNCTION__);
Expand Down
10 changes: 10 additions & 0 deletions test/etc/tasks/ext/phploc/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,21 @@
</phploc>
</target>

<target name="testReportJSON">
<phploc reportType="json"
reportDirectory="${project.basedir}">
<fileset dir=".">
<include name="*.php" />
</fileset>
</phploc>
</target>

<target name="testFormatters">
<phploc>
<formatter type="txt" toDir="${project.basedir}" outFile="phploc-report.txt" />
<formatter type="csv" toDir="${project.basedir}" outFile="phploc-report.csv" />
<formatter type="xml" toDir="${project.basedir}" outFile="phploc-report.xml" />
<formatter type="xml" toDir="${project.basedir}" outFile="phploc-report.json" />
<fileset dir=".">
<include name="*.php" />
</fileset>
Expand Down

0 comments on commit 8a6a501

Please sign in to comment.