Skip to content

Commit

Permalink
- Fixed #191: New implementation of --ignore only accepts relative pa…
Browse files Browse the repository at this point in the history
…ths.
  • Loading branch information
manuelpichler committed Jan 8, 2011
1 parent a073779 commit 38e6b52
Show file tree
Hide file tree
Showing 21 changed files with 380 additions and 56 deletions.
8 changes: 2 additions & 6 deletions CHANGELOG
@@ -1,9 +1,3 @@
PHP_Depend 0.11.0
-----------------

- Fixed #189: Invalid Start/End Line/Column for object method
invocation. Fixed in commit #c6cc9dd.

PHP_Depend 0.10.0
-----------------

Expand Down Expand Up @@ -41,6 +35,8 @@ PHP_Depend 0.10.0
(Unexpected token ASCII 39). Indirectly fixed in this release.
- Fixed #182: Clone is a valid function, method and type name in older
php versions. Fixed with git commit #b18bf37.
- Fixed #189: Invalid Start/End Line/Column for object method
invocation. Fixed in commit #c6cc9dd.

PHP_depend 0.9.20
-----------------
Expand Down
9 changes: 5 additions & 4 deletions PHP/Depend/Input/CompositeFilter.php
Expand Up @@ -84,17 +84,18 @@ public function append(PHP_Depend_Input_FilterI $filter)
* Delegates the given <b>$localPath</b> object to all aggregated filters.
* Returns <b>true</b> if this filter accepts the given path.
*
* @param string $localPath The local/relative path to the specified root.
* @param string $relative The relative path to the specified root.
* @param string $absolute The absolute path to a source file.
*
* @return boolean
*/
public function accept($localPath)
public function accept($relative, $absolute)
{
foreach ($this->filters as $filter) {
if (false === $filter->accept($localPath)) {
if (false === $filter->accept($relative, $absolute)) {
return false;
}
}
return true;
}
}
}
56 changes: 47 additions & 9 deletions PHP/Depend/Input/ExcludePathFilter.php
Expand Up @@ -61,11 +61,20 @@
class PHP_Depend_Input_ExcludePathFilter implements PHP_Depend_Input_FilterI
{
/**
* Regular expression that should not match against the file path.
* Regular expression that should not match against the relative file paths.
*
* @var string $regexp
* @var string
* @since 0.10.0
*/
protected $regexp = '';
protected $relative = '';

/**
* Regular expression that should not match against the absolute file paths.
*
* @var string
* @since 0.10.0
*/
protected $absolute = '';

/**
* Constructs a new exclude path filter instance and accepts an array of
Expand All @@ -75,21 +84,50 @@ class PHP_Depend_Input_ExcludePathFilter implements PHP_Depend_Input_FilterI
*/
public function __construct(array $patterns)
{
$regexp = join('|', array_map('preg_quote', $patterns));
$regexp = str_replace(array('\*'), array('.*'), $regexp);
$quoted = array_map('preg_quote', $patterns);

$this->regexp = "({$regexp})i";
$this->relative = '(' . str_replace('\*', '.*', join('|', $quoted)) . ')i';
$this->absolute = '(^' . str_replace('\*', '.*', join('|^', $quoted)) . ')i';
}

/**
* Returns <b>true</b> if this filter accepts the given path.
*
* @param string $localPath The local/relative path to the specified root.
* @param string $relative The relative path to the specified root.
* @param string $absolute The absolute path to a source file.
*
* @return boolean
*/
public function accept($relative, $absolute)
{
return ($this->notRelative($relative) && $this->notAbsolute($absolute));
}

/**
* This method checks if the given <b>$path</b> does not match against the
* exclude patterns as an absolute path.
*
* @param string $path The absolute path to a source file.
*
* @return boolean
* @since 0.10.0
*/
protected function notAbsolute($path)
{
return (preg_match($this->absolute, $path) === 0);
}

/**
* This method checks if the given <b>$path</b> does not match against the
* exclude patterns as an relative path.
*
* @param string $path The relative path to a source file.
*
* @return boolean
* @since 0.10.0
*/
public function accept($localPath)
protected function notRelative($path)
{
return (preg_match($this->regexp, $localPath) === 0);
return (preg_match($this->relative, $path) === 0);
}
}
9 changes: 5 additions & 4 deletions PHP/Depend/Input/ExtensionFilter.php
Expand Up @@ -79,15 +79,16 @@ public function __construct(array $extensions)
}

/**
* Returns <b>true</b> if this filter accepts the given path.
* Returns <b>true</b> if this filter accepts the given paths.
*
* @param string $localPath The local/relative path to the specified root.
* @param string $relative The relative path to the specified root.
* @param string $absolute The absolute path to a source file.
*
* @return boolean
*/
public function accept($localPath)
public function accept($relative, $absolute)
{
$extension = pathinfo($localPath, PATHINFO_EXTENSION);
$extension = pathinfo($relative, PATHINFO_EXTENSION);

return in_array($extension, $this->extensions);
}
Expand Down
7 changes: 4 additions & 3 deletions PHP/Depend/Input/FilterI.php
Expand Up @@ -61,11 +61,12 @@
interface PHP_Depend_Input_FilterI
{
/**
* Returns <b>true</b> if this filter accepts the given path.
* Returns <b>true</b> if this filter accepts the given paths.
*
* @param string $localPath The local/relative path to the specified root.
* @param string $relative The relative path to the specified root.
* @param string $absolute The absolute path to a source file.
*
* @return boolean
*/
function accept($localPath);
function accept($relative, $absolute);
}
26 changes: 18 additions & 8 deletions PHP/Depend/Input/Iterator.php
Expand Up @@ -78,16 +78,16 @@ class PHP_Depend_Input_Iterator extends FilterIterator
/**
* Constructs a new file filter iterator.
*
* @param Iterator $it The inner iterator.
* @param Iterator $iterator The inner iterator.
* @param PHP_Depend_Input_FilterI $filter The filter object.
* @param string $rootPath Optional root path for the files.
*/
public function __construct(
Iterator $it,
Iterator $iterator,
PHP_Depend_Input_FilterI $filter,
$rootPath = null
) {
parent::__construct($it);
parent::__construct($iterator);

$this->filter = $filter;
$this->rootPath = $rootPath;
Expand All @@ -100,7 +100,18 @@ public function __construct(
*/
public function accept()
{
return $this->filter->accept($this->getLocalPath());
return $this->filter->accept($this->getLocalPath(), $this->getFullPath());
}

/**
* Returns the full qualified realpath for the currently active file.
*
* @return string
* @since 0.10.0
*/
protected function getFullPath()
{
return $this->getInnerIterator()->current()->getRealpath();
}

/**
Expand All @@ -112,10 +123,9 @@ public function accept()
*/
protected function getLocalPath()
{
$localPath = $this->getInnerIterator()->current();
if ($this->rootPath && 0 === strpos($localPath, $this->rootPath)) {
$localPath = substr($localPath, strlen($this->rootPath));
if ($this->rootPath && 0 === strpos($this->getFullPath(), $this->rootPath)) {
return substr($this->getFullPath(), strlen($this->rootPath));
}
return $localPath;
return $this->getFullPath();
}
}
2 changes: 2 additions & 0 deletions tests/PHP/Depend/Bugs/AllTests.php
Expand Up @@ -85,6 +85,7 @@
require_once dirname(__FILE__) . '/ClassInterfaceSizeShouldNotSumComplexityBug176Test.php';
require_once dirname(__FILE__) . '/UnexpectedTokenAsciiChar39Bug181Test.php';
require_once dirname(__FILE__) . '/CloneIsValidNameInOlderPhpVersionsBug182Test.php';
require_once dirname(__FILE__) . '/ExcludePathFilterShouldFilterByAbsolutePathBug191Test.php';

/**
* Test suite for bugs meta package.
Expand Down Expand Up @@ -148,6 +149,7 @@ public static function suite()
$suite->addTestSuite('PHP_Depend_Bugs_ClassInterfaceSizeShouldNotSumComplexityBug176Test');
$suite->addTestSuite('PHP_Depend_Bugs_UnexpectedTokenAsciiChar39Bug181Test');
$suite->addTestSuite('PHP_Depend_Bugs_CloneIsValidNameInOlderPhpVersionsBug182Test');
$suite->addTestSuite('PHP_Depend_Input_ExcludePathFilterShouldFilterByAbsolutePathBug191Test');

return $suite;
}
Expand Down
@@ -0,0 +1,95 @@
<?php
/**
* This file is part of PHP_Depend.
*
* PHP Version 5
*
* Copyright (c) 2008-2010, Manuel Pichler <mapi@pdepend.org>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Manuel Pichler nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* 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.
*
* @category PHP
* @package PHP_Depend
* @subpackage Bugs
* @author Manuel Pichler <mapi@pdepend.org>
* @copyright 2008-2010 Manuel Pichler. All rights reserved.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://pdepend.org/
*/

require_once dirname(__FILE__) . '/AbstractTest.php';

/**
* Test case for bug #191.
*
* @category PHP
* @package PHP_Depend
* @subpackage Bugs
* @author Manuel Pichler <mapi@pdepend.org>
* @copyright 2008-2010 Manuel Pichler. All rights reserved.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version Release: @package_version@
* @link http://tracker.pdepend.org/pdepend/issue_tracker/issue/191
*
* @covers stdClass
*/
class PHP_Depend_Input_ExcludePathFilterShouldFilterByAbsolutePathBug191Test
extends PHP_Depend_Bugs_AbstractTest
{
/**
* testAbsoluteUnixPathAsFilterPattern
*
* @return void
* @group pdepend
* @group pdepend::bugs
* @group regressiontest
*/
public function testAbsoluteUnixPathAsFilterPattern()
{
$filter = new PHP_Depend_Input_ExcludePathFilter(array('/foo/bar'));
self::assertFalse($filter->accept('/baz', '/foo/bar/baz'));
}

/**
* testAbsoluteWindowsPathAsFilterPattern
*
* @return void
* @group pdepend
* @group pdepend::bugs
* @group regressiontest
*/
public function testAbsoluteWindowsPathAsFilterPattern()
{
$filter = new PHP_Depend_Input_ExcludePathFilter(array('c:\workspace\bar'));
self::assertFalse($filter->accept('\baz', 'c:\workspace\bar\baz'));
}
}
Expand Up @@ -78,10 +78,12 @@ public function testIteratorOnlyPassesLocalPathToFilter()
$filter = $this->getMock('PHP_Depend_Input_FilterI');
$filter->expects($this->once())
->method('accept')
->with(self::equalTo('/baz'));
->with(self::equalTo(DIRECTORY_SEPARATOR . basename(__FILE__)));

$iterator = new PHP_Depend_Input_Iterator(
new ArrayIterator(array('/foo/bar/baz')), $filter, '/foo/bar'
new ArrayIterator(array(new SplFileInfo(__FILE__))),
$filter,
dirname(__FILE__)
);
$iterator->accept();
}
Expand Down
6 changes: 3 additions & 3 deletions tests/PHP/Depend/Input/CompositeFilterTest.php
Expand Up @@ -80,7 +80,7 @@ public function testCompositeInvokesFirstAcceptInFilterChain()
$composite = new PHP_Depend_Input_CompositeFilter();
$composite->append($filter0);

$composite->accept(dirname(__FILE__));
$composite->accept(dirname(__FILE__), dirname(__FILE__));

$this->assertTrue($filter0->invoked);
}
Expand All @@ -102,7 +102,7 @@ public function testCompositeInvokesNextAcceptIfPreviousAcceptReturnsTrue()
$composite->append($filter0);
$composite->append($filter1);

$composite->accept(dirname(__FILE__));
$composite->accept(dirname(__FILE__), dirname(__FILE__));

$this->assertTrue($filter1->invoked);
}
Expand All @@ -124,7 +124,7 @@ public function testCompositeNotInvokesNextAcceptIfPreviousAcceptReturnsTrue()
$composite->append($filter0);
$composite->append($filter1);

$composite->accept(dirname(__FILE__));
$composite->accept(dirname(__FILE__), dirname(__FILE__));

$this->assertFalse($filter1->invoked);
}
Expand Down
7 changes: 4 additions & 3 deletions tests/PHP/Depend/Input/DummyFilter.php
Expand Up @@ -87,13 +87,14 @@ public function __construct($returnValue)
}

/**
* Returns <b>true</b> if this filter accepts the given path.
* Returns <b>true</b> if this filter accepts the given paths.
*
* @param string $localPath The local/relative path to the specified root.
* @param string $relative The relative path to the specified root.
* @param string $absolute The absolute path to a source file.
*
* @return boolean
*/
public function accept($localPath)
public function accept($relative, $absolute)
{
$this->invoked = true;

Expand Down

0 comments on commit 38e6b52

Please sign in to comment.