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

Scrutinizer issue fixes #1578

Merged
merged 1 commit into from
Apr 5, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 39 additions & 46 deletions src/Phing/Type/PatternSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function createExclude()
*
* @return PatternSetNameEntry Reference to object
*/
public function createExcludesFile()
public function createExcludesFile(): PatternSetNameEntry
{
if ($this->isReference()) {
throw $this->noChildrenAllowed();
Expand All @@ -134,16 +134,16 @@ public function createExcludesFile()
* Sets the set of include patterns. Patterns may be separated by a comma
* or a space.
*
* @param string $includes the string containing the include patterns
* @param string|null $includes the string containing the include patterns
*
* @throws BuildException
*/
public function setIncludes($includes)
public function setIncludes(?string $includes): void
{
if ($this->isReference()) {
throw $this->tooManyAttributes();
}
if (null !== $includes && strlen($includes) > 0) {
if (null !== $includes && '' !== $includes) {
$tok = strtok($includes, ', ');
while (false !== $tok) {
$o = $this->createInclude();
Expand All @@ -157,16 +157,16 @@ public function setIncludes($includes)
* Sets the set of exclude patterns. Patterns may be separated by a comma
* or a space.
*
* @param string $excludes the string containing the exclude patterns
* @param string|null $excludes the string containing the exclude patterns
*
* @throws BuildException
*/
public function setExcludes($excludes)
public function setExcludes(?string $excludes): void
{
if ($this->isReference()) {
throw $this->tooManyAttributes();
}
if (null !== $excludes && strlen($excludes) > 0) {
if (null !== $excludes && '' !== $excludes) {
$tok = strtok($excludes, ', ');
while (false !== $tok) {
$o = $this->createExclude();
Expand All @@ -183,16 +183,12 @@ public function setExcludes($excludes)
*
* @throws BuildException
*/
public function setIncludesFile($includesFile)
public function setIncludesFile(File $includesFile): void
{
if ($this->isReference()) {
throw $this->tooManyAttributes();
}
if ($includesFile instanceof File) {
$includesFile = $includesFile->getPath();
}
$o = $this->createIncludesFile();
$o->setName($includesFile);
$this->createIncludesFile()->setName($includesFile->getPath());
}

/**
Expand All @@ -202,24 +198,22 @@ public function setIncludesFile($includesFile)
*
* @throws BuildException
*/
public function setExcludesFile($excludesFile)
public function setExcludesFile(File $excludesFile): void
{
if ($this->isReference()) {
throw $this->tooManyAttributes();
}
if ($excludesFile instanceof File) {
$excludesFile = $excludesFile->getPath();
}
$o = $this->createExcludesFile();
$o->setName($excludesFile);
$this->createExcludesFile()->setName($excludesFile->getPath());
}

/**
* Adds the patterns of the other instance to this set.
*
* @throws BuildException
* @param PatternSet $other
* @param Project $p
* @throws IOException
*/
public function append(PatternSet $other, Project $p)
public function append(PatternSet $other, Project $p): void
{
if ($this->isReference()) {
throw new BuildException('Cannot append to a reference');
Expand All @@ -228,37 +222,35 @@ public function append(PatternSet $other, Project $p)
$incl = $other->getIncludePatterns($p);
if (null !== $incl) {
foreach ($incl as $incl_name) {
$o = $this->createInclude();
$o->setName($incl_name);
$this->createInclude()->setName($incl_name);
}
}

$excl = $other->getExcludePatterns($p);
if (null !== $excl) {
foreach ($excl as $excl_name) {
$o = $this->createExclude();
$o->setName($excl_name);
$this->createExclude()->setName($excl_name);
}
}
}

/**
* Returns the filtered include patterns.
*
* @throws BuildException
*
* @param Project $p
* @return array
* @throws IOException
*/
public function getIncludePatterns(Project $p)
public function getIncludePatterns(Project $p): ?array
{
if ($this->isReference()) {
$o = $this->getRef($p);

if ($o instanceof PatternSet) {
if ($o instanceof self) {
return $o->getIncludePatterns($p);
}

return [];
return null;
}

$this->readFiles($p);
Expand All @@ -269,20 +261,20 @@ public function getIncludePatterns(Project $p)
/**
* Returns the filtered exclude patterns.
*
* @throws BuildException
*
* @param Project $p
* @return array
* @throws IOException
*/
public function getExcludePatterns(Project $p)
public function getExcludePatterns(Project $p): ?array
{
if ($this->isReference()) {
$o = $this->getRef($p);

if ($o instanceof PatternSet) {
if ($o instanceof self) {
return $o->getExcludePatterns($p);
}

return [];
return null;
}

$this->readFiles($p);
Expand All @@ -305,8 +297,7 @@ public function hasPatterns()
* Performs the check for circular references and returns the
* referenced PatternSet.
*
* @throws BuildException
*
* @param Project $p
* @return Reference
*/
public function getRef(Project $p)
Expand All @@ -323,7 +314,7 @@ public function getRef(Project $p)
*
* @return PatternSetNameEntry Reference to the created PsetNameEntry instance
*/
private function addPatternToList(&$list)
private function addPatternToList(array &$list): PatternSetNameEntry
{
$num = array_push($list, new PatternSetNameEntry());

Expand All @@ -334,11 +325,12 @@ private function addPatternToList(&$list)
* Reads path matching patterns from a file and adds them to the
* includes or excludes list.
*
* @param File $patternfile
* @param array $patternlist
*
* @throws BuildException
* @param Project $p
* @throws IOException
*/
private function readPatterns(File $patternfile, &$patternlist, Project $p)
private function readPatterns(File $patternfile, array &$patternlist, Project $p): void
{
$patternReader = null;

Expand Down Expand Up @@ -372,10 +364,10 @@ private function readPatterns(File $patternfile, &$patternlist, Project $p)
* Convert a array of PatternSetNameEntry elements into an array of Strings.
*
* @param array $list
*
* @param Project $p
* @return array
*/
private function makeArray(&$list, Project $p)
private function makeArray(array $list, Project $p): ?array
{
if (0 === count($list)) {
return null;
Expand All @@ -384,7 +376,7 @@ private function makeArray(&$list, Project $p)
$tmpNames = [];
foreach ($list as $ne) {
$pattern = (string) $ne->evalName($p);
if (null !== $pattern && strlen($pattern) > 0) {
if (null !== $pattern && '' !== $pattern) {
$tmpNames[] = $pattern;
}
}
Expand All @@ -395,9 +387,10 @@ private function makeArray(&$list, Project $p)
/**
* Read includesfile or excludesfile if not already done so.
*
* @throws BuildException
* @param Project $p
* @throws IOException
*/
private function readFiles(Project $p)
private function readFiles(Project $p): void
{
if (!empty($this->includesFileList)) {
foreach ($this->includesFileList as $ne) {
Expand Down
27 changes: 14 additions & 13 deletions tests/Phing/Type/PatternSetTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
<?php

namespace Phing\Test\Type;

use Phing\Exception\BuildException;
use Phing\Project;
use Phing\Type\PatternSet;
use Phing\Type\Reference;
use PHPUnit\Framework\TestCase;

/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Expand All @@ -25,6 +17,15 @@
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/

namespace Phing\Test\Type;

use Phing\Exception\BuildException;
use Phing\Project;
use Phing\Type\PatternSet;
use Phing\Type\Reference;
use PHPUnit\Framework\TestCase;

class PatternSetTest extends TestCase
{
private $patternset;
Expand All @@ -34,14 +35,14 @@ protected function setUp(): void
$this->patternset = new PatternSet();
}

public function testBothEmpty()
public function testBothEmpty(): void
{
$s = '' . $this->patternset;
$this->assertEquals('patternSet{ includes: empty excludes: empty }', $s);
$this->assertEquals(false, $this->patternset->hasPatterns());
}

public function testIfReferenceSetThenCreateIncludeThrowsException()
public function testIfReferenceSetThenCreateIncludeThrowsException(): void
{
$project = new Project();
$reference = new Reference($project);
Expand All @@ -53,7 +54,7 @@ public function testIfReferenceSetThenCreateIncludeThrowsException()
$this->patternset->createInclude();
}

public function testIfReferenceSetThenCreateExcludeThrowsException()
public function testIfReferenceSetThenCreateExcludeThrowsException(): void
{
$project = new Project();
$reference = new Reference($project);
Expand All @@ -65,7 +66,7 @@ public function testIfReferenceSetThenCreateExcludeThrowsException()
$this->patternset->createExclude();
}

public function testIfReferencesSetThenCreatExcludesFileThrowsException()
public function testIfReferencesSetThenCreatExcludesFileThrowsException(): void
{
$project = new Project();
$reference = new Reference($project);
Expand All @@ -77,7 +78,7 @@ public function testIfReferencesSetThenCreatExcludesFileThrowsException()
$this->patternset->createExcludesFile();
}

public function testIfReferencesSetThenCreatIncludesFileThrowsException()
public function testIfReferencesSetThenCreatIncludesFileThrowsException(): void
{
$project = new Project();
$reference = new Reference($project);
Expand Down