Skip to content

Commit 1190179

Browse files
authored
Merge pull request #903 from frankdekker/Improve_baseline_basepath_calculation
Improve baseline basepath calculation
2 parents 42fb77b + 18d2246 commit 1190179

File tree

11 files changed

+61
-123
lines changed

11 files changed

+61
-123
lines changed

README.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,14 @@ Command line options
133133
violations are found.
134134

135135
- ``--generate-baseline`` - will generate a ``phpmd.baseline.xml`` for existing violations
136-
next to the ruleset definition file.
136+
next to the ruleset definition file. The file paths of the violations will be relative to the current
137+
working directory.
137138

138139
- ``--update-baseline`` - will remove all violations from an existing ``phpmd.baseline.xml``
139-
that no longer exist. New violations will _not_ be added.
140+
that no longer exist. New violations will _not_ be added. The file path of the violations will be relative
141+
to the current working directory.
140142

141-
- ``--baseline-file`` - the filepath to a custom baseline xml file. The filepath
142-
of all baselined files must be relative to this file location.
143+
- ``--baseline-file`` - the filepath to a custom baseline xml file.
143144

144145
An example command line: ::
145146

src/main/php/PHPMD/Baseline/BaselineSet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function contains($ruleName, $fileName, $methodName)
2828
$fileName = str_replace('\\', '/', $fileName);
2929

3030
foreach ($this->violations[$ruleName] as $baseline) {
31-
if ($baseline->getFileName() === $fileName && $baseline->getMethodName() === $methodName) {
31+
if ($baseline->matches($fileName, $methodName)) {
3232
return true;
3333
}
3434
}

src/main/php/PHPMD/Baseline/BaselineSetFactory.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace PHPMD\Baseline;
44

5-
use PHPMD\Utility\Paths;
65
use RuntimeException;
76

87
class BaselineSetFactory
@@ -26,9 +25,7 @@ public static function fromFile($fileName)
2625
throw new RuntimeException('Unable to read xml from: ' . $fileName);
2726
}
2827

29-
$basePath = dirname($fileName);
3028
$baselineSet = new BaselineSet();
31-
3229
foreach ($xml->children() as $node) {
3330
if ($node->getName() !== 'violation') {
3431
continue;
@@ -42,14 +39,12 @@ public static function fromFile($fileName)
4239
throw new RuntimeException('Missing `file` attribute in `violation` in ' . $fileName);
4340
}
4441

45-
$ruleName = (string)$node['rule'];
46-
$filePath = Paths::concat($basePath, (string)$node['file']);
4742
$methodName = null;
4843
if (isset($node['method']) === true && ((string)$node['method']) !== '') {
4944
$methodName = (string)($node['method']);
5045
}
5146

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

5550
return $baselineSet;

src/main/php/PHPMD/Baseline/ViolationBaseline.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class ViolationBaseline
1010
/** @var string */
1111
private $fileName;
1212

13+
/** @var int */
14+
private $fileNameLength;
15+
1316
/** @var string|null */
1417
private $methodName;
1518

@@ -20,9 +23,10 @@ class ViolationBaseline
2023
*/
2124
public function __construct($ruleName, $fileName, $methodName)
2225
{
23-
$this->ruleName = $ruleName;
24-
$this->fileName = $fileName;
25-
$this->methodName = $methodName;
26+
$this->ruleName = $ruleName;
27+
$this->fileName = $fileName;
28+
$this->methodName = $methodName;
29+
$this->fileNameLength = strlen($fileName);
2630
}
2731

2832
/**
@@ -34,18 +38,15 @@ public function getRuleName()
3438
}
3539

3640
/**
37-
* @return string
38-
*/
39-
public function getFileName()
40-
{
41-
return $this->fileName;
42-
}
43-
44-
/**
45-
* @return string|null
41+
* Test if the given filepath and method matches the baseline
42+
*
43+
* @param string $filepath the full filepath to match against
44+
* @param string|null $methodName the name of the method of the method, if any
45+
*
46+
* @return bool
4647
*/
47-
public function getMethodName()
48+
public function matches($filepath, $methodName)
4849
{
49-
return $this->methodName;
50+
return $this->methodName === $methodName && substr($filepath, -$this->fileNameLength) === $this->fileName;
5051
}
5152
}

src/main/php/PHPMD/Renderer/RendererFactory.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace PHPMD\Renderer;
44

5-
use PHPMD\Utility\Paths;
65
use PHPMD\Writer\StreamWriter;
76
use RuntimeException;
87

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

2220
return $renderer;

src/main/php/PHPMD/Utility/Paths.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,6 @@
66

77
class Paths
88
{
9-
/**
10-
* Append $pathB to $pathA and apply the correct amount of slashes between them
11-
*
12-
* @param string $pathA
13-
* @param string $pathB
14-
* @return string
15-
*/
16-
public static function concat($pathA, $pathB)
17-
{
18-
$pathA = rtrim(str_replace('\\', '/', $pathA), '/');
19-
$pathB = ltrim(str_replace('\\', '/', $pathB), '/');
20-
return $pathA . '/' . $pathB;
21-
}
22-
239
/**
2410
* Transform the given absolute path to the relative path based on the given base path.
2511
*
@@ -41,18 +27,6 @@ public static function getRelativePath($basePath, $filePath)
4127
return $filePath;
4228
}
4329

44-
/**
45-
* Derive the absolute path from the given resource
46-
* @param resource $resource
47-
* @return string
48-
* @throws RuntimeException
49-
*/
50-
public static function getAbsolutePath($resource)
51-
{
52-
$metaData = stream_get_meta_data($resource);
53-
return self::getRealPath($metaData['uri']);
54-
}
55-
5630
/**
5731
* Get the realpath of the given path or exception on failure
5832
* @param string $path

src/site/rst/documentation/index.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,14 @@ Command line options
6565
violations are found.
6666

6767
- ``--generate-baseline`` - will generate a ``phpmd.baseline.xml`` for existing violations
68-
next to the ruleset definition file.
68+
next to the ruleset definition file. The file paths of the violations will be relative to the current
69+
working directory.
6970

7071
- ``--update-baseline`` - will remove all violations from an existing ``phpmd.baseline.xml``
71-
that no longer exist. New violations will _not_ be added.
72+
that no longer exist. New violations will _not_ be added. The file path of the violations will be relative
73+
to the current working directory.
7274

73-
- ``--baseline-file`` - the filepath to a custom baseline xml file. The filepath
74-
of all baselined files must be relative to this file location.
75+
- ``--baseline-file`` - the filepath to a custom baseline xml file.
7576

7677
An example command line: ::
7778

src/test/php/PHPMD/Baseline/ViolationBaselineTest.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,42 @@ class ViolationBaselineTest extends TestCase
1212
/**
1313
* @covers ::__construct
1414
* @covers ::getRuleName
15-
* @covers ::getFileName
16-
* @covers ::getMethodName
1715
*/
18-
public function testAccessorsWithoutMethod()
16+
public function testGetRuleName()
1917
{
2018
$violation = new ViolationBaseline('rule', 'foobar', null);
2119
static::assertSame('rule', $violation->getRuleName());
22-
static::assertSame('foobar', $violation->getFileName());
23-
static::assertNull($violation->getMethodName());
2420
}
2521

2622
/**
23+
* Test the give file matches the baseline correctly
24+
*
2725
* @covers ::__construct
28-
* @covers ::getMethodName
26+
* @covers ::matches
27+
* @return void
2928
*/
30-
public function testAccessorsWithMethod()
29+
public function testMatchesWithMethod()
3130
{
32-
$violation = new ViolationBaseline('rule', 'foobar', 'method');
33-
static::assertSame('method', $violation->getMethodName());
31+
$violation = new ViolationBaseline('sniff', 'foobar.txt', 'method');
32+
static::assertTrue($violation->matches('foobar.txt', 'method'));
33+
static::assertTrue($violation->matches('/test/foobar.txt', 'method'));
34+
static::assertFalse($violation->matches('foo.txt', 'method'));
35+
static::assertFalse($violation->matches('foobar.txt', 'unknown'));
36+
}
37+
38+
/**
39+
* Test the give file matches the baseline correctly
40+
*
41+
* @covers ::__construct
42+
* @covers ::matches
43+
* @return void
44+
*/
45+
public function testMatchesWithoutMethod()
46+
{
47+
$violation = new ViolationBaseline('sniff', 'foobar.txt', null);
48+
static::assertTrue($violation->matches('foobar.txt', null));
49+
static::assertTrue($violation->matches('/test/foobar.txt', null));
50+
static::assertFalse($violation->matches('foobar.txt', 'method'));
51+
static::assertFalse($violation->matches('/test/unknown.txt', null));
3452
}
3553
}

src/test/php/PHPMD/Renderer/RendererFactoryTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use PHPMD\AbstractTest;
66
use PHPMD\Writer\StreamWriter;
7-
use RuntimeException;
87

98
/**
109
* @coversDefaultClass \PHPMD\Renderer\RendererFactory
@@ -21,15 +20,4 @@ public function testCreateBaselineRendererSuccessfully()
2120

2221
static::assertSame($writer, $renderer->getWriter());
2322
}
24-
25-
/**
26-
* @covers ::createBaselineRenderer
27-
* @expectedException RuntimeException
28-
* @expectedExceptionMessage Unable to determine the realpath for
29-
*/
30-
public function testCreateBaselineRendererThrowsExceptionForInvalidStream()
31-
{
32-
$writer = new StreamWriter(STDOUT);
33-
RendererFactory::createBaselineRenderer($writer);
34-
}
3523
}

src/test/php/PHPMD/TextUI/CommandTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace PHPMD\TextUI;
1919

2020
use PHPMD\AbstractTest;
21+
use PHPMD\Utility\Paths;
2122

2223
/**
2324
* Test case for the {@link \PHPMD\TextUI\Command} class.
@@ -246,7 +247,7 @@ public function testMainGenerateBaseline()
246247

247248
static::assertSame(Command::EXIT_SUCCESS, $exitCode);
248249
static::assertFileExists($temp);
249-
static::assertContains($uri, file_get_contents($temp));
250+
static::assertContains(Paths::getRelativePath(getcwd(), $uri), file_get_contents($temp));
250251
}
251252

252253
/**
@@ -262,6 +263,9 @@ public function testMainUpdateBaseline()
262263
{
263264
$sourceTemp = self::createTempFileUri('ClassWithMultipleViolations.php');
264265
$baselineTemp = self::createTempFileUri();
266+
// set work directory to the temp dir
267+
self::changeWorkingDirectory(dirname($baselineTemp));
268+
265269
copy(static::createResourceUriForTest('UpdateBaseline/ClassWithMultipleViolations.php'), $sourceTemp);
266270
copy(static::createResourceUriForTest('UpdateBaseline/phpmd.baseline.xml'), $baselineTemp);
267271

src/test/php/PHPMD/Utility/PathsTest.php

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,6 @@
1010
*/
1111
class PathsTest extends AbstractTest
1212
{
13-
/**
14-
* @covers ::concat
15-
*/
16-
public function testConcatShouldConcatTwoPaths()
17-
{
18-
static::assertSame('/foo/bar', Paths::concat('/foo', '/bar'));
19-
}
20-
21-
/**
22-
* @covers ::concat
23-
*/
24-
public function testConcatShouldDeduplicateSlashes()
25-
{
26-
static::assertSame('/foo/bar', Paths::concat('/foo/', '/bar'));
27-
}
28-
29-
/**
30-
* @covers ::concat
31-
*/
32-
public function testConcatShouldForwardAllSlashes()
33-
{
34-
static::assertSame('/foo/bar/text.txt', Paths::concat('/foo\\', '/bar\\text.txt'));
35-
}
36-
3713
/**
3814
* @covers ::getRelativePath
3915
*/
@@ -58,24 +34,6 @@ public function testGetRelativePathShouldNotSubtractOnInfixPath()
5834
static::assertSame('/foo/bar/text.txt', Paths::getRelativePath('/bar', '/foo/bar/text.txt'));
5935
}
6036

61-
/**
62-
* @covers ::getAbsolutePath
63-
* @expectedException RuntimeException
64-
*/
65-
public function testGetAbsolutePathShouldReturnNullForIrregularStream()
66-
{
67-
Paths::getAbsolutePath(fopen('php://stdout', 'wb'));
68-
}
69-
70-
/**
71-
* @covers ::getAbsolutePath
72-
*/
73-
public function testGetAbsolutePathShouldReturnPath()
74-
{
75-
$path = static::createResourceUriForTest('resource.txt');
76-
static::assertSame(realpath($path), Paths::getAbsolutePath(fopen($path, 'rb')));
77-
}
78-
7937
/**
8038
* @covers ::getRealPath
8139
*/

0 commit comments

Comments
 (0)