Skip to content

Commit

Permalink
Disable xdebug for all php processes except code coverage generator (#85
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sidz authored and maks-rafalko committed Nov 29, 2017
1 parent 7d738bf commit 47c8350
Show file tree
Hide file tree
Showing 21 changed files with 764 additions and 111 deletions.
3 changes: 2 additions & 1 deletion .php_cs.dist
Expand Up @@ -14,6 +14,7 @@ return \PhpCsFixer\Config::create()
'phpdoc_summary' => false,
'phpdoc_align' => false,
'yoda_style' => false,
'is_null' => true,
])
->setFinder($finder)
;
;
23 changes: 22 additions & 1 deletion app/bootstrap.php
Expand Up @@ -4,6 +4,9 @@
*
* License: https://opensource.org/licenses/BSD-3-Clause New BSD License
*/
use Infection\Php\XdebugHandler;
use Infection\Php\ConfigBuilder;

$files = [
__DIR__ . '/../../../autoload.php',
__DIR__ . '/../vendor/autoload.php',
Expand All @@ -18,8 +21,11 @@
}
}

unset($files);

if (!defined('INFECTION_COMPOSER_INSTALL')) {
fwrite(STDERR,
fwrite(
STDERR,
'You need to set up the project dependencies using Composer:' . PHP_EOL . PHP_EOL .
' composer install' . PHP_EOL . PHP_EOL .
'You can learn all about Composer on https://getcomposer.org/.' . PHP_EOL
Expand All @@ -30,4 +36,19 @@

require INFECTION_COMPOSER_INSTALL;

$isDebuggerDisabled = empty((string) getenv(XdebugHandler::ENV_DISABLE_XDEBUG));

$xdebug = new XdebugHandler(new ConfigBuilder(sys_get_temp_dir()));
$xdebug->check();
unset($xdebug);

if (PHP_SAPI !== 'phpdbg' && $isDebuggerDisabled && !extension_loaded('xdebug')) {
fwrite(
STDERR,
'You need to use phpdbg or install and enable xdebug in order to allow for code coverage generation.' . PHP_EOL
);

die(1);
}

$container = require __DIR__ . '/container.php';
15 changes: 7 additions & 8 deletions app/container.php
Expand Up @@ -12,7 +12,6 @@
use Infection\TestFramework\Factory;
use Infection\Differ\Differ;
use Infection\Mutant\MutantCreator;
use Infection\Command\InfectionCommand;
use Infection\Process\Runner\Parallel\ParallelProcessRunner;
use Infection\EventDispatcher\EventDispatcher;
use Infection\Filesystem\Filesystem;
Expand All @@ -26,6 +25,7 @@
use Infection\TestFramework\PhpUnit\Adapter\PhpUnitAdapter;
use Infection\Config\InfectionConfig;
use Infection\Utils\VersionParser;
use Infection\Command;
use Infection\TestFramework\Coverage\CachedTestFileDataProvider;
use Infection\TestFramework\PhpUnit\Config\XmlConfigurationHelper;
use SebastianBergmann\Diff\Differ as BaseDiffer;
Expand Down Expand Up @@ -54,7 +54,7 @@
return $c['temp.dir.creator']->createAndGet();
};

$c['filesystem'] = function (Container $c): Filesystem {
$c['filesystem'] = function (): Filesystem {
return new Filesystem();
};

Expand Down Expand Up @@ -138,14 +138,14 @@
return (new ParserFactory())->create(ParserFactory::PREFER_PHP7, $c['lexer']);
};

$c['pretty.printer'] = function ($c): Standard {
$c['pretty.printer'] = function (): Standard {
return new Standard();
};

function registerMutators(array $mutators, Container $container)
{
foreach ($mutators as $mutator) {
$container[$mutator] = function (Container $c) use ($mutator) {
$container[$mutator] = function () use ($mutator) {
return new $mutator();
};
}
Expand All @@ -158,11 +158,10 @@ function registerMutators(array $mutators, Container $container)
'Infection - PHP Mutation Testing Framework',
'@package_version@'
);
$infectionCommand = new InfectionCommand($container);

$application->add(new \Infection\Command\ConfigureCommand());
$application->add(new \Infection\Command\SelfUpdateCommand());
$application->add($infectionCommand);
$application->add(new Command\ConfigureCommand());
$application->add(new Command\SelfUpdateCommand());
$application->add(new Command\InfectionCommand($container));

return $application;
};
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions src/Command/InfectionCommand.php
Expand Up @@ -177,10 +177,6 @@ protected function initialize(InputInterface $input, OutputInterface $output)
throw new \Exception('Configuration aborted');
}
}

if (PHP_SAPI !== 'phpdbg' && !defined('HHVM_VERSION') && !extension_loaded('xdebug')) {
throw new \Exception('You need to use phpdbg or install and enable xDebug in order to allow for code coverage generation.');
}
}

private function setOutputFormatterStyles(OutputInterface $output)
Expand Down
28 changes: 12 additions & 16 deletions src/Finder/AbstractExecutableFinder.php
Expand Up @@ -8,46 +8,42 @@

namespace Infection\Finder;

use Symfony\Component\Process\PhpExecutableFinder;
use Infection\Process\ExecutableFinder\PhpExecutableFinder;

abstract class AbstractExecutableFinder
{
/**
* @return string
*/
abstract public function find();
abstract public function find(bool $includeArgs = true): string;

/**
* @param array $probableNames
* @param array $extraDirectories
* @param bool $includeArgs
*
* @return string|null
*/
protected function searchNonExecutables(array $probableNames, array $extraDirectories = [])
protected function searchNonExecutables(array $probableNames, array $extraDirectories = [], bool $includeArgs = true)
{
$dirs = array_merge(
explode(PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')),
$extraDirectories
);

foreach ($dirs as $dir) {
foreach ($probableNames as $name) {
$path = sprintf('%s/%s', $dir, $name);
if (file_exists($path)) {
return $this->makeExecutable($path);
return $this->makeExecutable($path, $includeArgs);
}
}
}
}

/**
* @param string $path
*
* @return string
*/
protected function makeExecutable($path)
protected function makeExecutable(string $path, bool $includeArgs = true): string
{
$phpFinder = new PhpExecutableFinder();

return sprintf('%s %s', $phpFinder->find(), $path);
return sprintf(
'%s %s',
(new PhpExecutableFinder())->find($includeArgs),
$path
);
}
}
13 changes: 4 additions & 9 deletions src/Finder/ComposerExecutableFinder.php
Expand Up @@ -10,14 +10,9 @@

use Symfony\Component\Process\ExecutableFinder;

class ComposerExecutableFinder extends AbstractExecutableFinder
final class ComposerExecutableFinder extends AbstractExecutableFinder
{
/**
* @return string
*
* @throws \Exception
*/
public function find()
public function find(bool $includeArgs = true): string
{
$probable = ['composer', 'composer.phar'];
$finder = new ExecutableFinder();
Expand All @@ -33,9 +28,9 @@ public function find()
* Check for options without execute permissions and prefix the PHP
* executable instead.
*/
$result = $this->searchNonExecutables($probable, $immediatePaths);
$result = $this->searchNonExecutables($probable, $immediatePaths, $includeArgs);

if (!is_null($result)) {
if (null !== $result) {
return $result;
}

Expand Down
52 changes: 23 additions & 29 deletions src/Finder/TestFrameworkExecutableFinder.php
Expand Up @@ -9,8 +9,8 @@
namespace Infection\Finder;

use Infection\Finder\Exception\TestFrameworkExecutableFinderNotFound;
use Infection\Process\ExecutableFinder\PhpExecutableFinder;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;

class TestFrameworkExecutableFinder extends AbstractExecutableFinder
Expand All @@ -21,7 +21,7 @@ class TestFrameworkExecutableFinder extends AbstractExecutableFinder
private $cachedExecutable;

/**
* @var
* @var string
*/
private $testFrameworkName;

Expand All @@ -30,25 +30,29 @@ class TestFrameworkExecutableFinder extends AbstractExecutableFinder
*/
private $customPath;

/**
* @var bool
*/
private $cachedIncludedArgs;

public function __construct(string $testFrameworkName, string $customPath = null)
{
$this->testFrameworkName = $testFrameworkName;
$this->customPath = $customPath;
}

/**
* @return string
*/
public function find()
public function find(bool $includeArgs = true): string
{
if ($this->cachedExecutable === null) {
if ($this->cachedExecutable === null || $this->cachedIncludedArgs !== $includeArgs) {
if (!$this->doesCustomPathExist()) {
$this->addVendorFolderToPath();
}

$this->cachedExecutable = $this->findExecutable();
$this->cachedExecutable = $this->findExecutable($includeArgs);
}

$this->cachedIncludedArgs = $includeArgs;

return $this->cachedExecutable;
}

Expand All @@ -66,25 +70,17 @@ private function addVendorFolderToPath()
}
}

if (!is_null($vendorPath)) {
if (null !== $vendorPath) {
putenv('PATH=' . $vendorPath . PATH_SEPARATOR . getenv('PATH'));
}
}

/**
* @return string
*/
private function findComposer()
private function findComposer(): string
{
return (new ComposerExecutableFinder())->find();
}

/**
* @return string
*
* @throws TestFrameworkExecutableFinderNotFound
*/
private function findExecutable()
private function findExecutable(bool $includeArgs = true): string
{
if ($this->doesCustomPathExist()) {
return $this->makeExecutable($this->customPath);
Expand All @@ -95,13 +91,13 @@ private function findExecutable()

foreach ($candidates as $name) {
if ($path = $finder->find($name, null, [getcwd()])) {
return $this->makeExecutable($path);
return $this->makeExecutable($path, $includeArgs);
}
}

$result = $this->searchNonExecutables($candidates, [getcwd()]);
$result = $this->searchNonExecutables($candidates, [getcwd()], $includeArgs);

if (!is_null($result)) {
if (null !== $result) {
return $result;
}

Expand All @@ -119,28 +115,26 @@ private function findExecutable()
* are enforced and end PHP processes properly
*
* @param string $path
* @param bool $includeArgs
*
* @return string
*/
protected function makeExecutable($path)
protected function makeExecutable(string $path, bool $includeArgs = true): string
{
$path = realpath($path);
$phpFinder = new PhpExecutableFinder();

if (defined('PHP_WINDOWS_VERSION_BUILD')) {
if (\defined('PHP_WINDOWS_VERSION_BUILD')) {
if (false !== strpos($path, '.bat')) {
return $path;
}

return sprintf('%s %s', $phpFinder->find(), $path);
return sprintf('%s %s', $phpFinder->find($includeArgs), $path);
}

return sprintf('%s %s %s', 'exec', $phpFinder->find(), $path);
return sprintf('%s %s %s', 'exec', $phpFinder->find($includeArgs), $path);
}

/**
* @return bool
*/
private function doesCustomPathExist(): bool
{
return $this->customPath && file_exists($this->customPath);
Expand Down

0 comments on commit 47c8350

Please sign in to comment.