Missing dependancies
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of PHP CS Fixer.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace PhpCsFixer\Linter;
|
||||
|
||||
/**
|
||||
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class CachingLinter implements LinterInterface
|
||||
{
|
||||
private LinterInterface $sublinter;
|
||||
|
||||
/**
|
||||
* @var array<string, LintingResultInterface>
|
||||
*/
|
||||
private array $cache = [];
|
||||
|
||||
public function __construct(LinterInterface $linter)
|
||||
{
|
||||
$this->sublinter = $linter;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isAsync(): bool
|
||||
{
|
||||
return $this->sublinter->isAsync();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function lintFile(string $path): LintingResultInterface
|
||||
{
|
||||
$checksum = md5(file_get_contents($path));
|
||||
|
||||
if (!isset($this->cache[$checksum])) {
|
||||
$this->cache[$checksum] = $this->sublinter->lintFile($path);
|
||||
}
|
||||
|
||||
return $this->cache[$checksum];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function lintSource(string $source): LintingResultInterface
|
||||
{
|
||||
$checksum = md5($source);
|
||||
|
||||
if (!isset($this->cache[$checksum])) {
|
||||
$this->cache[$checksum] = $this->sublinter->lintSource($source);
|
||||
}
|
||||
|
||||
return $this->cache[$checksum];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of PHP CS Fixer.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace PhpCsFixer\Linter;
|
||||
|
||||
/**
|
||||
* Handle PHP code linting process.
|
||||
*
|
||||
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class Linter implements LinterInterface
|
||||
{
|
||||
private LinterInterface $subLinter;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->subLinter = new TokenizerLinter();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isAsync(): bool
|
||||
{
|
||||
return $this->subLinter->isAsync();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function lintFile(string $path): LintingResultInterface
|
||||
{
|
||||
return $this->subLinter->lintFile($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function lintSource(string $source): LintingResultInterface
|
||||
{
|
||||
return $this->subLinter->lintSource($source);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of PHP CS Fixer.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace PhpCsFixer\Linter;
|
||||
|
||||
/**
|
||||
* Interface for PHP code linting process manager.
|
||||
*
|
||||
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*/
|
||||
interface LinterInterface
|
||||
{
|
||||
public function isAsync(): bool;
|
||||
|
||||
/**
|
||||
* Lint PHP file.
|
||||
*/
|
||||
public function lintFile(string $path): LintingResultInterface;
|
||||
|
||||
/**
|
||||
* Lint PHP code.
|
||||
*/
|
||||
public function lintSource(string $source): LintingResultInterface;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of PHP CS Fixer.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace PhpCsFixer\Linter;
|
||||
|
||||
/**
|
||||
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* @final
|
||||
*
|
||||
* @TODO 4.0 make class "final"
|
||||
*/
|
||||
class LintingException extends \RuntimeException
|
||||
{
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of PHP CS Fixer.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace PhpCsFixer\Linter;
|
||||
|
||||
/**
|
||||
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*/
|
||||
interface LintingResultInterface
|
||||
{
|
||||
/**
|
||||
* Check if linting process was successful and raise LintingException if not.
|
||||
*/
|
||||
public function check(): void;
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of PHP CS Fixer.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace PhpCsFixer\Linter;
|
||||
|
||||
use PhpCsFixer\FileReader;
|
||||
use PhpCsFixer\FileRemoval;
|
||||
use Symfony\Component\Filesystem\Exception\IOException;
|
||||
use Symfony\Component\Process\PhpExecutableFinder;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
/**
|
||||
* Handle PHP code linting using separated process of `php -l _file_`.
|
||||
*
|
||||
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class ProcessLinter implements LinterInterface
|
||||
{
|
||||
private FileRemoval $fileRemoval;
|
||||
|
||||
private ProcessLinterProcessBuilder $processBuilder;
|
||||
|
||||
/**
|
||||
* Temporary file for code linting.
|
||||
*
|
||||
* @var null|string
|
||||
*/
|
||||
private $temporaryFile;
|
||||
|
||||
/**
|
||||
* @param null|string $executable PHP executable, null for autodetection
|
||||
*/
|
||||
public function __construct(?string $executable = null)
|
||||
{
|
||||
if (null === $executable) {
|
||||
$executableFinder = new PhpExecutableFinder();
|
||||
$executable = $executableFinder->find(false);
|
||||
|
||||
if (false === $executable) {
|
||||
throw new UnavailableLinterException('Cannot find PHP executable.');
|
||||
}
|
||||
|
||||
if ('phpdbg' === \PHP_SAPI) {
|
||||
if (!str_contains($executable, 'phpdbg')) {
|
||||
throw new UnavailableLinterException('Automatically found PHP executable is non-standard phpdbg. Could not find proper PHP executable.');
|
||||
}
|
||||
|
||||
// automatically found executable is `phpdbg`, let us try to fallback to regular `php`
|
||||
$executable = str_replace('phpdbg', 'php', $executable);
|
||||
|
||||
if (!is_executable($executable)) {
|
||||
throw new UnavailableLinterException('Automatically found PHP executable is phpdbg. Could not find proper PHP executable.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->processBuilder = new ProcessLinterProcessBuilder($executable);
|
||||
$this->fileRemoval = new FileRemoval();
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
if (null !== $this->temporaryFile) {
|
||||
$this->fileRemoval->delete($this->temporaryFile);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This class is not intended to be serialized,
|
||||
* and cannot be deserialized (see __wakeup method).
|
||||
*/
|
||||
public function __sleep(): array
|
||||
{
|
||||
throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable the deserialization of the class to prevent attacker executing
|
||||
* code by leveraging the __destruct method.
|
||||
*
|
||||
* @see https://owasp.org/www-community/vulnerabilities/PHP_Object_Injection
|
||||
*/
|
||||
public function __wakeup(): void
|
||||
{
|
||||
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isAsync(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function lintFile(string $path): LintingResultInterface
|
||||
{
|
||||
return new ProcessLintingResult($this->createProcessForFile($path), $path);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function lintSource(string $source): LintingResultInterface
|
||||
{
|
||||
return new ProcessLintingResult($this->createProcessForSource($source), $this->temporaryFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path path to file
|
||||
*/
|
||||
private function createProcessForFile(string $path): Process
|
||||
{
|
||||
// in case php://stdin
|
||||
if (!is_file($path)) {
|
||||
return $this->createProcessForSource(FileReader::createSingleton()->read($path));
|
||||
}
|
||||
|
||||
$process = $this->processBuilder->build($path);
|
||||
$process->setTimeout(10);
|
||||
$process->start();
|
||||
|
||||
return $process;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create process that lint PHP code.
|
||||
*
|
||||
* @param string $source code
|
||||
*/
|
||||
private function createProcessForSource(string $source): Process
|
||||
{
|
||||
if (null === $this->temporaryFile) {
|
||||
$this->temporaryFile = tempnam(sys_get_temp_dir(), 'cs_fixer_tmp_');
|
||||
$this->fileRemoval->observe($this->temporaryFile);
|
||||
}
|
||||
|
||||
if (false === @file_put_contents($this->temporaryFile, $source)) {
|
||||
throw new IOException(sprintf('Failed to write file "%s".', $this->temporaryFile), 0, null, $this->temporaryFile);
|
||||
}
|
||||
|
||||
return $this->createProcessForFile($this->temporaryFile);
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of PHP CS Fixer.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace PhpCsFixer\Linter;
|
||||
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
/**
|
||||
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class ProcessLinterProcessBuilder
|
||||
{
|
||||
private string $executable;
|
||||
|
||||
/**
|
||||
* @param string $executable PHP executable
|
||||
*/
|
||||
public function __construct(string $executable)
|
||||
{
|
||||
$this->executable = $executable;
|
||||
}
|
||||
|
||||
public function build(string $path): Process
|
||||
{
|
||||
return new Process([
|
||||
$this->executable,
|
||||
'-l',
|
||||
$path,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of PHP CS Fixer.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace PhpCsFixer\Linter;
|
||||
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
/**
|
||||
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class ProcessLintingResult implements LintingResultInterface
|
||||
{
|
||||
private Process $process;
|
||||
|
||||
private ?string $path;
|
||||
|
||||
private ?bool $isSuccessful = null;
|
||||
|
||||
public function __construct(Process $process, ?string $path = null)
|
||||
{
|
||||
$this->process = $process;
|
||||
$this->path = $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function check(): void
|
||||
{
|
||||
if (!$this->isSuccessful()) {
|
||||
// on some systems stderr is used, but on others, it's not
|
||||
throw new LintingException($this->getProcessErrorMessage(), $this->process->getExitCode());
|
||||
}
|
||||
}
|
||||
|
||||
private function getProcessErrorMessage(): string
|
||||
{
|
||||
$output = strtok(ltrim($this->process->getErrorOutput() ?: $this->process->getOutput()), "\n");
|
||||
|
||||
if (false === $output) {
|
||||
return 'Fatal error: Unable to lint file.';
|
||||
}
|
||||
|
||||
if (null !== $this->path) {
|
||||
$needle = sprintf('in %s ', $this->path);
|
||||
$pos = strrpos($output, $needle);
|
||||
|
||||
if (false !== $pos) {
|
||||
$output = sprintf('%s%s', substr($output, 0, $pos), substr($output, $pos + \strlen($needle)));
|
||||
}
|
||||
}
|
||||
|
||||
$prefix = substr($output, 0, 18);
|
||||
|
||||
if ('PHP Parse error: ' === $prefix) {
|
||||
return sprintf('Parse error: %s.', substr($output, 18));
|
||||
}
|
||||
|
||||
if ('PHP Fatal error: ' === $prefix) {
|
||||
return sprintf('Fatal error: %s.', substr($output, 18));
|
||||
}
|
||||
|
||||
return sprintf('%s.', $output);
|
||||
}
|
||||
|
||||
private function isSuccessful(): bool
|
||||
{
|
||||
if (null === $this->isSuccessful) {
|
||||
$this->process->wait();
|
||||
$this->isSuccessful = $this->process->isSuccessful();
|
||||
}
|
||||
|
||||
return $this->isSuccessful;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of PHP CS Fixer.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace PhpCsFixer\Linter;
|
||||
|
||||
use PhpCsFixer\FileReader;
|
||||
use PhpCsFixer\Tokenizer\CodeHasher;
|
||||
use PhpCsFixer\Tokenizer\Tokens;
|
||||
|
||||
/**
|
||||
* Handle PHP code linting.
|
||||
*
|
||||
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class TokenizerLinter implements LinterInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isAsync(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function lintFile(string $path): LintingResultInterface
|
||||
{
|
||||
return $this->lintSource(FileReader::createSingleton()->read($path));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function lintSource(string $source): LintingResultInterface
|
||||
{
|
||||
try {
|
||||
// To lint, we will parse the source into Tokens.
|
||||
// During that process, it might throw a ParseError or CompileError.
|
||||
// If it won't, cache of tokenized version of source will be kept, which is great for Runner.
|
||||
// Yet, first we need to clear already existing cache to not hit it and lint the code indeed.
|
||||
$codeHash = CodeHasher::calculateCodeHash($source);
|
||||
Tokens::clearCache($codeHash);
|
||||
Tokens::fromCode($source);
|
||||
|
||||
return new TokenizerLintingResult();
|
||||
} catch (\ParseError|\CompileError $e) {
|
||||
return new TokenizerLintingResult($e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of PHP CS Fixer.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace PhpCsFixer\Linter;
|
||||
|
||||
/**
|
||||
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class TokenizerLintingResult implements LintingResultInterface
|
||||
{
|
||||
private ?\Error $error;
|
||||
|
||||
public function __construct(?\Error $error = null)
|
||||
{
|
||||
$this->error = $error;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function check(): void
|
||||
{
|
||||
if (null !== $this->error) {
|
||||
throw new LintingException(
|
||||
sprintf('%s: %s on line %d.', $this->getMessagePrefix(), $this->error->getMessage(), $this->error->getLine()),
|
||||
$this->error->getCode(),
|
||||
$this->error
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private function getMessagePrefix(): string
|
||||
{
|
||||
return $this->error instanceof \ParseError ? 'Parse error' : 'Fatal error';
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of PHP CS Fixer.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace PhpCsFixer\Linter;
|
||||
|
||||
/**
|
||||
* Exception that is thrown when the chosen linter is not available on the environment.
|
||||
*
|
||||
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* @final
|
||||
*
|
||||
* @TODO 4.0 make class "final"
|
||||
*/
|
||||
class UnavailableLinterException extends \RuntimeException
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user