diff --git a/README.md b/README.md index b0a1039..0651109 100644 --- a/README.md +++ b/README.md @@ -23,3 +23,13 @@ ## Интеграция с pull request Добавить webhook в atlassian stash с указанием ссылки на index.php из phpcs-bitbucket с аргументами index.php?branch=${refChange.refId}&repo=${project.key}&slug=${repository.slug} + +#Конфигурация + +Блок [phpvardumpcheck] поддерживает: + +``` +className='PhpCsBitBucket\Checker\PhpVarDumpCheck' ;Class to check +mode='--symfony' ; mode (more - https://github.com/JakubOnderka/PHP-Var-Dump-Check#options-for-run) +skipFunctions='var_export' ; functions, that will be skiped +``` \ No newline at end of file diff --git a/composer.json b/composer.json index fc9a538..7faf316 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,12 @@ { "name": "whotrades/bitbucket-code-style", "type": "library", - "author": "Artem Naumenko", + "authors": [ + { + "name": "Artem Naumenko", + "role": "Developer" + } + ], "description": "Robot for integration code style checking at atlassian bitbucket (supports phpcs and cpplint)", "autoload": { "psr-4": { @@ -13,6 +18,7 @@ "squizlabs/php_codesniffer": "3.*", "guzzlehttp/guzzle": "~6.3", "monolog/monolog": "~1.23", - "php": ">=7.0" + "php": ">=7.0", + "jakub-onderka/php-var-dump-check": "^0.3.0" } } diff --git a/composer.lock b/composer.lock index b676850..ee0dea1 100644 --- a/composer.lock +++ b/composer.lock @@ -1,10 +1,10 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "4b0c615bfb4435c1b5770b1443dd199b", + "content-hash": "8cc69eb1b0f5173df7032c834c1c1184", "packages": [ { "name": "guzzlehttp/guzzle", @@ -187,6 +187,52 @@ ], "time": "2017-03-20T17:10:46+00:00" }, + { + "name": "jakub-onderka/php-var-dump-check", + "version": "v0.3", + "source": { + "type": "git", + "url": "https://github.com/JakubOnderka/PHP-Var-Dump-Check.git", + "reference": "c7b30cbe73b7815811d079cb5bc326c313dd084e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/JakubOnderka/PHP-Var-Dump-Check/zipball/c7b30cbe73b7815811d079cb5bc326c313dd084e", + "reference": "c7b30cbe73b7815811d079cb5bc326c313dd084e", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "require-dev": { + "jakub-onderka/php-parallel-lint": "~1.0", + "phpunit/phpunit": "~4.5" + }, + "suggest": { + "jakub-onderka/php-console-highlighter": "For colored console output" + }, + "bin": [ + "var-dump-check" + ], + "type": "library", + "autoload": { + "psr-4": { + "JakubOnderka\\PhpVarDumpCheck\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-2-Clause" + ], + "authors": [ + { + "name": "Jakub Onderka", + "email": "jakub.onderka@gmail.com" + } + ], + "description": "Find forgotten variables dump in PHP source code.", + "time": "2018-09-29T19:22:45+00:00" + }, { "name": "monolog/monolog", "version": "1.23.0", diff --git a/configuration.ini-dist b/configuration.ini-dist index 9b49e07..bc02893 100644 --- a/configuration.ini-dist +++ b/configuration.ini-dist @@ -10,7 +10,7 @@ httpTimeout = 90 [checkers] ; coma separated list of checkers -list=phpcs +list=phpcs,phpvardumpcheck [phpcs] className=PhpCsBitBucket\Checker\PhpCs @@ -23,6 +23,10 @@ standard='vendor/squizlabs/php_codesniffer/src/Standards/PSR2/ruleset.xml' ; Пути установки дополнительных стилей кодирования installed_paths='' +[phpvardumpcheck] +className='PhpCsBitBucket\Checker\PhpVarDumpCheck' +mode='--symfony' + [cpp] className=PhpCsBitBucket\Checker\Cpp ; Путь к cpplint diff --git a/lib/Checker/PhpVarDumpCheck.php b/lib/Checker/PhpVarDumpCheck.php new file mode 100644 index 0000000..bdcf6a0 --- /dev/null +++ b/lib/Checker/PhpVarDumpCheck.php @@ -0,0 +1,117 @@ + + * + * @copyright © 2011-2019 WhoTrades, Ltd. (http://whotrades.com). All rights reserved. + */ + +namespace PhpCsBitBucket\Checker; + +use Monolog\Logger; +use PhpCsBitBucket\CheckerResult\CheckerResultItem; +use PhpCsBitBucket\CheckerResult\CheckerResultItemInterface; + +class PhpVarDumpCheck implements CheckerInterface +{ + /** + * @var Logger + */ + private $log; + + /** + * @var array + */ + private $config; + + /** + * @var string + */ + private $tmpDir; + + /** + * @param Logger $log + * @param array $config + */ + public function __construct(Logger $log, array $config) + { + $this->log = $log; + $this->config = $config; + $this->tmpDir = $config['tmpdir'] ?? '/tmp'; + } + + /** + * @param string $filename + * @param string $extension + * + * @return bool + */ + public function shouldIgnoreFile($filename, $extension) + { + if (!empty($this->config['extensions'])) { + $checkOnly = explode(',', $this->config['extensions']); + + return !in_array($extension, $checkOnly); + } + + return false; + } + + /** + * @param string $filePath + * + * @return \JakubOnderka\PhpVarDumpCheck\Settings + * + * @throws \JakubOnderka\PhpVarDumpCheck\Exception\InvalidArgument + */ + private function getToolConfig(string $filePath) + { + $config = [0]; + if (isset($this->config['mode'])) { + $config[] = $this->config['mode']; + } + + $settings = \JakubOnderka\PhpVarDumpCheck\Settings::parseArguments(array_merge($config, [$filePath])); + + if (isset($this->config['skipFunctions'])) { + $skipFunctions = explode(',', $this->config['skipFunctions']); + foreach ($skipFunctions as $function) { + if (($key = array_search($function, $settings->functionsToCheck)) !== false) { + unset($settings->functionsToCheck[$key]); + } + } + } + + return $settings; + } + + /** + * @param string $filename + * @param string $extension + * @param string $fileContent + * + * @return CheckerResultItemInterface[] + * + * @throws \JakubOnderka\PhpVarDumpCheck\Exception\InvalidArgument + */ + public function processFile($filename, $extension, $fileContent) + { + // prepare an temp file for catch + $tempFile = "$this->tmpDir/temp.$extension"; + file_put_contents($tempFile, $fileContent); + $result = []; + try { + $check = new \JakubOnderka\PhpVarDumpCheck\Manager(); + $status = $check->checkFile($tempFile, $this->getToolConfig($tempFile)); + foreach ($status as $item) { + $result[] = new CheckerResultItem($item->getLineNumber(), 'Forgotten dump found'); + } + } catch (\Exception $e) { + $this->log->addCritical($e->getMessage(), $e->getTrace()); + } + + unlink($tempFile); + + return $result; + } +} \ No newline at end of file