Skip to content

Commit

Permalink
Merge pull request #24 from mediact/issue/21
Browse files Browse the repository at this point in the history
1.0.4 - Prevent autoloading targets of static analysis
  • Loading branch information
sjokki authored Sep 4, 2018
2 parents ab8b7ec + e897803 commit 6a99bd2
Show file tree
Hide file tree
Showing 14 changed files with 347 additions and 44 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
"php": "^7.1",
"composer/composer": "^1.6",
"composer-plugin-api": "^1.1",
"nikic/php-parser": "^3.0 | ^4.0"
"nikic/php-parser": "^3.0 | ^4.0",
"roave/better-reflection": "^3.1"
},
"require-dev": {
"symfony/process": "@stable",
"mediact/testing-suite": "@stable",
"mikey179/vfsstream": "^1.6",
"phpro/grumphp": "~0.1",
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<testsuite name="default">
<directory>tests</directory>
</testsuite>
<testsuite name="regression">
<directory>tests/Regression</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
Expand Down
4 changes: 2 additions & 2 deletions src/Candidate/CandidateExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Mediact\DependencyGuard\Php\SymbolInterface;
use Mediact\DependencyGuard\Php\SymbolIterator;
use Mediact\DependencyGuard\Php\SymbolIteratorInterface;
use ReflectionClass;
use Roave\BetterReflection\Reflection\ReflectionClass;

class CandidateExtractor implements CandidateExtractorInterface
{
Expand Down Expand Up @@ -100,7 +100,7 @@ private function extractPackage(
$name = $symbol->getName();

if (!array_key_exists($name, $packagesPerSymbol)) {
$reflection = new ReflectionClass($name);
$reflection = ReflectionClass::createFromName($name);
$file = $reflection->getFileName();

// This happens for symbols in the current package.
Expand Down
29 changes: 3 additions & 26 deletions src/Composer/Command/DependencyGuardCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
namespace Mediact\DependencyGuard\Composer\Command;

use Composer\Command\BaseCommand;
use Composer\Composer;
use Mediact\DependencyGuard\Composer\Command\Exporter\ViolationExporterFactory;
use Mediact\DependencyGuard\Composer\Command\Exporter\ViolationExporterFactoryInterface;
use Mediact\DependencyGuard\DependencyGuardFactory;
Expand Down Expand Up @@ -85,37 +84,15 @@ protected function execute(
InputInterface $input,
OutputInterface $output
): int {
$composer = $this->getComposer(true);
$guard = $this->guardFactory->create();

$this->registerAutoloader($composer);
$composer = $this->getComposer(true);
$guard = $this->guardFactory->create();
$violations = $guard->determineViolations($composer);
$exporter = $this->exporterFactory->create($input, $output);

$exporter = $this->exporterFactory->create($input, $output);
$exporter->export($violations);

return count($violations) > 0
? static::EXIT_VIOLATIONS
: static::EXIT_NO_VIOLATIONS;
}

/**
* Register the autoloader for the current project, so subject classes can
* be automatically loaded.
*
* @param Composer $composer
*
* @return void
*/
private function registerAutoloader(Composer $composer): void
{
$config = $composer->getConfig();
$vendor = $config->get('vendor-dir', 0);
$autoloader = $vendor . DIRECTORY_SEPARATOR . 'autoload.php';

if (is_readable($autoloader)) {
/** @noinspection PhpIncludeInspection */
require_once $autoloader;
}
}
}
4 changes: 2 additions & 2 deletions src/Php/Filter/UserDefinedSymbolFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Mediact\DependencyGuard\Php\Filter;

use ReflectionClass;
use Roave\BetterReflection\Reflection\ReflectionClass;
use Throwable;

class UserDefinedSymbolFilter implements SymbolFilterInterface
Expand All @@ -21,7 +21,7 @@ class UserDefinedSymbolFilter implements SymbolFilterInterface
public function __invoke(string $symbol): bool
{
try {
$reflection = new ReflectionClass($symbol);
$reflection = ReflectionClass::createFromName($symbol);
} catch (Throwable $e) {
return false;
}
Expand Down
10 changes: 9 additions & 1 deletion tests/Composer/Command/DependencyGuardCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Composer\Composer;
use Composer\Config;
use Composer\EventDispatcher\EventDispatcher;
use Mediact\DependencyGuard\Composer\Command\Exporter\ViolationExporterFactoryInterface;
use Mediact\DependencyGuard\DependencyGuardFactoryInterface;
use Mediact\DependencyGuard\DependencyGuardInterface;
Expand All @@ -22,6 +23,7 @@

/**
* @coversDefaultClass \Mediact\DependencyGuard\Composer\Command\DependencyGuardCommand
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class DependencyGuardCommandTest extends TestCase
{
Expand Down Expand Up @@ -64,6 +66,13 @@ private function createComposer(string $vendorDir): Composer
->method('getConfig')
->willReturn($config);

$composer
->expects(self::any())
->method('getEventDispatcher')
->willReturn(
$this->createMock(EventDispatcher::class)
);

$config
->expects(self::any())
->method('get')
Expand All @@ -82,7 +91,6 @@ private function createComposer(string $vendorDir): Composer
* @return void
*
* @covers ::execute
* @covers ::registerAutoloader
*/
public function testExecute(
ViolationIteratorInterface $violations,
Expand Down
4 changes: 2 additions & 2 deletions tests/GrumPHP/DependencyGuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function testCanRunInContext(
}

/**
* @return ContextInterface[][]|bool[][]
* @return array
*/
public function contextProvider(): array
{
Expand Down Expand Up @@ -223,7 +223,7 @@ private function createViolations(
}

/**
* @return DependencyGuardInterface[][]|bool[][]
* @return array
*/
public function guardProvider(): array
{
Expand Down
6 changes: 3 additions & 3 deletions tests/Php/SymbolExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private function createFileIterator(
}

/**
* @return Parser[][]|FileIteratorInterface[][]
* @return array
*/
public function emptyProvider(): array
{
Expand Down Expand Up @@ -164,7 +164,7 @@ private function createFile(string $content): SplFileInfo
}

/**
* @return Parser[][]|FileIteratorInterface[][]
* @return array
*/
public function nonParsingFilesProvider(): array
{
Expand All @@ -191,7 +191,7 @@ public function nonParsingFilesProvider(): array
}

/**
* @return Parser[][]|FileIteratorInterface[][]
* @return array
*/
public function filledFilesProvider(): array
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Php/SymbolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function testConstructor(): void
}

/**
* @return Name[][]|string[][]
* @return array
*/
public function nameProvider(): array
{
Expand Down Expand Up @@ -126,7 +126,7 @@ public function testToString(Name $name, string $expected): void
}

/**
* @return Name[][]|string[][]
* @return array
*/
public function lineProvider(): array
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Php/SymbolTrackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private function createNode(string $name = null): Node
}

/**
* @return SymbolFilterInterface[][]|Node[][][]|Node[][]
* @return array
*/
public function whitelistedNodesProvider(): array
{
Expand Down Expand Up @@ -100,7 +100,7 @@ public function whitelistedNodesProvider(): array
}

/**
* @return SymbolFilterInterface[][]|Node[][][]|Node[][]
* @return array
*/
public function blacklistedNodesProvider(): array
{
Expand Down
65 changes: 65 additions & 0 deletions tests/Regression/Issue21/Issue21Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Mediact\DependencyGuard\Tests\Regression\Issue21;

use Composer\Util\Filesystem;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;

/**
* @see https://github.com/mediact/dependency-guard/issues/21
*/
class Issue21Test extends TestCase
{
/**
* @return void
*/
public function setUp(): void
{
$process = new Process(
[
trim(`which composer` ?? `which composer.phar`),
'install',
'--quiet',
'--working-dir',
__DIR__
]
);

if ($process->run() !== 0) {
self::markTestSkipped(
$process->getErrorOutput()
);
}
}

/**
* @return void
* @coversNothing
*/
public function testNoFatalsBecauseOfConflicts(): void
{
$process = new Process(
[
PHP_BINARY,
'../../../bin/dependency-guard'
],
__DIR__
);

$process->run();

self::assertNotContains('Fatal error:', $process->getErrorOutput());
}

/**
* @return void
*/
public function tearDown(): void
{
$filesystem = new Filesystem();
$filesystem->removeDirectory(
__DIR__ . DIRECTORY_SEPARATOR . 'vendor'
);
}
}
6 changes: 6 additions & 0 deletions tests/Regression/Issue21/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "issue21/test",
"require": {
"symfony/console": "2.8.45"
}
}
Loading

0 comments on commit 6a99bd2

Please sign in to comment.