Skip to content

Commit

Permalink
Merge pull request #31 from Idrinth/prevent-regex-breaking-on-windows
Browse files Browse the repository at this point in the history
Fix: Windows path seperators break some regexes
  • Loading branch information
ashokadewit authored Nov 28, 2018
2 parents a82b192 + 40a1f52 commit 678f111
Show file tree
Hide file tree
Showing 10 changed files with 861 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/mediact/dependency-guard/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/mediact/dependency-guard/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/mediact/dependency-guard/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/mediact/dependency-guard/?branch=master)
[![Build Status](https://scrutinizer-ci.com/g/mediact/dependency-guard/badges/build.png?b=master)](https://scrutinizer-ci.com/g/mediact/dependency-guard/build-status/master)
[![Build status](https://ci.appveyor.com/api/projects/status/79f486l0u1p2i5gq/branch/master?svg=true)](https://ci.appveyor.com/project/mediactbv/dependency-guard/branch/master)
[![Code Intelligence Status](https://scrutinizer-ci.com/g/mediact/dependency-guard/badges/code-intelligence.svg?b=master)](https://scrutinizer-ci.com/code-intelligence)
[![license](https://img.shields.io/github/license/mediact/dependency-guard.png)](LICENSE.md)

Expand Down
43 changes: 43 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
build: off
platform: [x64]
clone_folder: c:\projects\php-project-workspace

init:
- SET PATH=C:\Program Files\OpenSSL;c:\tools\php;%PATH%
- SET COMPOSER_NO_INTERACTION=1
- SET PHP=1
- SET ANSICON=121x90 (121x90)

environment:
matrix:
- dependencies: lowest
php_ver_target: 7.1
- dependencies: current
php_ver_target: 7.2

cache:
- composer.phar
- C:\ProgramData\chocolatey\bin -> .appveyor.yml
- C:\ProgramData\chocolatey\lib -> .appveyor.yml
- c:\tools\php -> .appveyor.yml

install:
- IF EXIST c:\tools\php (SET PHP=0)
- ps: appveyor-retry cinst --params '""/InstallDir:C:\tools\php""' --ignore-checksums -y php --version ((choco search php --exact --all-versions -r | select-string -pattern $env:php_ver_target | sort { [version]($_ -split '\|' | select -last 1) } -Descending | Select-Object -first 1) -replace '[php|]','')
- cd c:\tools\php
- IF %PHP%==1 copy php.ini-production php.ini /Y
- IF %PHP%==1 echo date.timezone="UTC" >> php.ini
- IF %PHP%==1 echo extension_dir=ext >> php.ini
- IF %PHP%==1 echo extension=php_openssl.dll >> php.ini
- IF %PHP%==1 echo extension=php_mbstring.dll >> php.ini
- IF %PHP%==1 echo extension=php_fileinfo.dll >> php.ini
- IF %PHP%==1 echo @php %%~dp0composer.phar %%* > composer.bat
- appveyor-retry appveyor DownloadFile https://getcomposer.org/composer.phar
- cd c:\projects\php-project-workspace
- del /f composer.lock
- appveyor-retry composer update --no-progress --profile
- composer show

test_script:
- cd c:\projects\php-project-workspace
- vendor/bin/grumphp run
5 changes: 5 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="default">
<directory>tests/Regression</directory>
<directory>tests</directory>
</testsuite>
<testsuite name="coverage">
<directory>tests</directory>
<exclude>tests/Regression</exclude>
</testsuite>
<testsuite name="regression">
<directory>tests/Regression</directory>
Expand Down
8 changes: 4 additions & 4 deletions src/Candidate/CandidateExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function extract(
SymbolIteratorInterface $symbols
): iterable {
$repository = $composer->getRepositoryManager()->getLocalRepository();
$vendorPath = $composer->getConfig()->get('vendor-dir', 0);
$vendorPath = str_replace('\\', '/', $composer->getConfig()->get('vendor-dir', 0));

$packages = [];

Expand Down Expand Up @@ -103,19 +103,19 @@ private function extractPackage(

if (!array_key_exists($name, $packagesPerSymbol)) {
$reflection = $this->getClassReflection($name);
$file = $reflection->getFileName();
$file = str_replace('\\', '/', $reflection->getFileName());

// This happens for symbols in the current package.
if (strpos($file, $vendorPath) !== 0) {
return null;
}

$structure = explode(
DIRECTORY_SEPARATOR,
'/',
preg_replace(
sprintf(
'/^%s/',
preg_quote($vendorPath . DIRECTORY_SEPARATOR, '/')
preg_quote($vendorPath . '/', '/')
),
'',
$file
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/Exporter/TextViolationExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(
*/
public function export(ViolationIteratorInterface $violations): void
{
$root = $this->workingDirectory . DIRECTORY_SEPARATOR;
$root = preg_quote($this->workingDirectory . '/', '#');

foreach ($violations as $violation) {
$this->prompt->error($violation->getMessage());
Expand Down
45 changes: 34 additions & 11 deletions src/Composer/Iterator/SourceFileIteratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,26 +141,21 @@ private function createClassmapIterator(
);
}

return new class ($files, ...$exclude) extends FilterIterator {
return new class ($files, $this->preparePattern(...$exclude)) extends FilterIterator {
/** @var string|null */
private $excludePattern;

/**
* Constructor.
*
* @param Iterator $iterator
* @param string ...$excludePatterns
* @param Iterator $iterator
* @param string|null $excludePattern
*/
public function __construct(
Iterator $iterator,
string ...$excludePatterns
?string $excludePattern
) {
if (!empty($excludePatterns)) {
$this->excludePattern = sprintf(
'@^(%s)$@',
implode('|', $excludePatterns)
);
}
$this->excludePattern = $excludePattern;

parent::__construct($iterator);
}
Expand All @@ -181,11 +176,39 @@ public function accept(): bool
$this->excludePattern === null
?: !preg_match(
$this->excludePattern,
$file->getRealPath()
str_replace('\\', '/', $file->getRealPath())
)
)
);
}
};
}

/**
* @param string ...$excludePatterns
*
* @return string|null
*/
private function preparePattern(string ...$excludePatterns): ?string
{
if (count($excludePatterns) === 0) {
return null;
}

return sprintf(
'@^(%s)$@',
implode(
'|',
array_map(
function (string $pattern): string {
return preg_quote(
str_replace('\\', '/', $pattern),
'@'
);
},
$excludePatterns
)
)
);
}
}
1 change: 1 addition & 0 deletions tests/Composer/Iterator/SourceFileIteratorFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class SourceFileIteratorFactoryTest extends TestCase
* @covers ::createFilesIterator
* @covers ::createNamespaceIterator
* @covers ::createClassmapIterator
* @covers ::preparePattern
*/
public function testCreate(Composer $composer): void
{
Expand Down
Loading

0 comments on commit 678f111

Please sign in to comment.