Skip to content

Commit

Permalink
Remove absolute path from BaselineSet and adjust baseline matching
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdekker committed Jul 17, 2021
1 parent 7f87839 commit c8d6ba3
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/main/php/PHPMD/Baseline/BaselineSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function contains($ruleName, $fileName, $methodName)
$fileName = str_replace('\\', '/', $fileName);

foreach ($this->violations[$ruleName] as $baseline) {
if ($baseline->getFileName() === $fileName && $baseline->getMethodName() === $methodName) {
if ($baseline->matches($fileName, $methodName)) {
return true;
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/main/php/PHPMD/Baseline/BaselineSetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ public static function fromFile($fileName)
throw new RuntimeException('Unable to read xml from: ' . $fileName);
}

$basePath = dirname($fileName);
$baselineSet = new BaselineSet();

foreach ($xml->children() as $node) {
if ($node->getName() !== 'violation') {
continue;
Expand All @@ -42,14 +40,12 @@ public static function fromFile($fileName)
throw new RuntimeException('Missing `file` attribute in `violation` in ' . $fileName);
}

$ruleName = (string)$node['rule'];
$filePath = Paths::concat($basePath, (string)$node['file']);
$methodName = null;
if (isset($node['method']) === true && ((string)$node['method']) !== '') {
$methodName = (string)($node['method']);
}

$baselineSet->addEntry(new ViolationBaseline($ruleName, $filePath, $methodName));
$baselineSet->addEntry(new ViolationBaseline((string)$node['rule'], (string)$node['file'], $methodName));
}

return $baselineSet;
Expand Down
29 changes: 15 additions & 14 deletions src/main/php/PHPMD/Baseline/ViolationBaseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class ViolationBaseline
/** @var string */
private $fileName;

/** @var int */
private $fileNameLength;

/** @var string|null */
private $methodName;

Expand All @@ -20,9 +23,10 @@ class ViolationBaseline
*/
public function __construct($ruleName, $fileName, $methodName)
{
$this->ruleName = $ruleName;
$this->fileName = $fileName;
$this->methodName = $methodName;
$this->ruleName = $ruleName;
$this->fileName = $fileName;
$this->methodName = $methodName;
$this->fileNameLength = strlen($fileName);
}

/**
Expand All @@ -34,18 +38,15 @@ public function getRuleName()
}

/**
* @return string
*/
public function getFileName()
{
return $this->fileName;
}

/**
* @return string|null
* Test if the given filepath and method matches the baseline
*
* @param string $filepath the full filepath to match against
* @param string|null $methodName the name of the method of the method, if any
*
* @return bool
*/
public function getMethodName()
public function matches($filepath, $methodName)
{
return $this->methodName;
return $this->methodName === $methodName && substr($filepath, -$this->fileNameLength) === $this->fileName;
}
}
6 changes: 2 additions & 4 deletions src/main/php/PHPMD/Renderer/RendererFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PHPMD\Renderer;

use PHPMD\Utility\Paths;
use PHPMD\Writer\StreamWriter;
use RuntimeException;

Expand All @@ -14,9 +13,8 @@ class RendererFactory
*/
public static function createBaselineRenderer(StreamWriter $writer)
{
// determine basedir based on stream output filepath
$absolutePath = Paths::getAbsolutePath($writer->getStream());
$renderer = new BaselineRenderer(dirname($absolutePath));
// set base path to current working directory
$renderer = new BaselineRenderer(getcwd());
$renderer->setWriter($writer);

return $renderer;
Expand Down
26 changes: 0 additions & 26 deletions src/main/php/PHPMD/Utility/Paths.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@

class Paths
{
/**
* Append $pathB to $pathA and apply the correct amount of slashes between them
*
* @param string $pathA
* @param string $pathB
* @return string
*/
public static function concat($pathA, $pathB)
{
$pathA = rtrim(str_replace('\\', '/', $pathA), '/');
$pathB = ltrim(str_replace('\\', '/', $pathB), '/');
return $pathA . '/' . $pathB;
}

/**
* Transform the given absolute path to the relative path based on the given base path.
*
Expand All @@ -41,18 +27,6 @@ public static function getRelativePath($basePath, $filePath)
return $filePath;
}

/**
* Derive the absolute path from the given resource
* @param resource $resource
* @return string
* @throws RuntimeException
*/
public static function getAbsolutePath($resource)
{
$metaData = stream_get_meta_data($resource);
return self::getRealPath($metaData['uri']);
}

/**
* Get the realpath of the given path or exception on failure
* @param string $path
Expand Down

0 comments on commit c8d6ba3

Please sign in to comment.