add phpstan action module (partially)

This commit is contained in:
Jean-Christian Denis 2021-11-07 00:55:24 +01:00
parent e2d880af27
commit 9a90725d1a
Signed by: JcDenis
GPG key ID: 1B5B8C5B90B6C951
5 changed files with 174 additions and 0 deletions

View file

@ -23,6 +23,7 @@ $core->addBehavior('improveAddAction', ['ImproveActionLicensefile', 'create']);
$core->addBehavior('improveAddAction', ['ImproveActionNewline', 'create']);
$core->addBehavior('improveAddAction', ['ImproveActionPhpcsfixer', 'create']);
$core->addBehavior('improveAddAction', ['ImproveActionPhpheader', 'create']);
$core->addBehavior('improveAddAction', ['ImproveActionPhpstan', 'create']);
$core->addBehavior('improveAddAction', ['ImproveActionTab', 'create']);
$core->addBehavior('improveAddAction', ['ImproveActionZip', 'create']);

View file

@ -26,6 +26,7 @@ $improve_libs = [
'ImproveActionNewline' => 'lib.improve.action.php',
'ImproveActionPhpcsfixer' => 'lib.improve.action.phpcsfixer.php',
'ImproveActionPhpheader' => 'lib.improve.action.phpheader.php',
'ImproveActionPhpstan' => 'lib.improve.action.phpstan.php',
'ImproveActionTab' => 'lib.improve.action.php',
'ImproveActionZip' => 'lib.improve.action.zip.php',
'ImproveZipFileZip' => 'lib.improve.action.zip.php'

View file

@ -0,0 +1,131 @@
<?php
/**
* @brief improve, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
class ImproveActionPhpstan extends ImproveAction
{
protected function init(): bool
{
$this->setProperties([
'id' => 'phpstan',
'name' => __('PHPStan'),
'desc' => __('Analyse php code using PHPStan'),
'priority' => 910,
'config' => true,
'types' => ['plugin']
]);
return true;
}
public function isConfigured(): bool
{
return true;
}
public function configure($url): ?string
{
if (!empty($_POST['save'])) {
$this->setSettings([
'phpexe_path' => (!empty($_POST['phpexe_path']) ? $_POST['phpexe_path'] : ''),
'run_level' => (integer) $_POST['run_level']
]);
$this->redirect($url);
}
return
'<p><label class="classic" for="phpexe_path">' .
__('Root directory of PHP executable:') . '<br />' .
form::field('phpexe_path', 160, 255, $this->getSetting('phpexe_path')) . '</label>' .
'</p>' .
'<p class="form-note">' .
__('If this module does not work you can try to put here directory to php executable (without executable file name).') .
' C:\path_to\php</p>' .
'<p><label class="classic" for="run_level">' . __('Level:') . ' </label>' .
form::number('run_level', ['min' => 0, 'max' => 9, 'default' => (integer) $this->getSetting('run_level')]) . '</p>' .
'<p class="info">' . __('You must enable improve details to view analyse results !') . '</p>';
}
public function closeModule(): ?bool
{
$phpexe_path = $this->getPhpPath();
if (!empty($phpexe_path)) {
$phpexe_path .= '/';
}
if (!$this->writeConf()) {
$this->setError(__('Failed to write phpstan configuration'));
return false;
}
$command = sprintf(
'%sphp %s/libs/phpstan.phar analyse --configuration=%s',
$phpexe_path,
dirname(__FILE__),
DC_VAR . '/phpstan.neon'
);
try {
exec($command, $output, $error);
if (!empty($error) && empty($output)) {
throw new Exception('oops');
}
if (empty($output)) {
$output[] = __('No errors found');
}
$this->setSuccess(sprintf('<pre>%s</pre>', implode('<br />', $output)));
return true;
} catch (Exception $e) {
$this->setError(__('Failed to run phpstan'));
return false;
}
}
private function getPhpPath()
{
$phpexe_path = $this->getSetting('phpexe_path');
if (empty($phpexe_path) && !empty(PHP_BINDIR)) {
$phpexe_path = PHP_BINDIR;
}
return path::real($phpexe_path);
}
private function writeConf()
{
$content =
"parameters:\n" .
" level: " . (integer) $this->getSetting('run_level') . "\n\n" .
" paths: \n" .
" - " . $this->module['sroot'] . "\n\n" .
" scanFiles:\n" .
" - " . DC_ROOT . "/index.php\n" .
" scanDirectories:\n" .
" - " . DC_ROOT . "\n" .
" excludePaths:\n" .
" - " . $this->module['sroot'] . "/*/libs/*\n\n" .
" bootstrapFiles:\n" .
" - " . dirname(__FILE__) . "/libs/dc.phpstan.bootstrap.php\n\n" .
" fileExtensions:\n" .
" - php\n" .
" - in\n\n" .
// extra
" checkMissingIterableValueType: false\n" .
" checkGenericClassInNonGenericObjectType: false\n";
return file_put_contents(DC_VAR . '/phpstan.neon', $content);
}
}

View file

@ -0,0 +1,41 @@
<?php
// Mock some global constants
define('DC_ADMIN_SSL', true);
define('DC_ADMIN_URL', '');
define('DC_ALLOW_MULTI_MODULES', true);
define('DC_ALLOW_REPOSITORIES', true);
define('DC_CRYPT_ALGO', 'md5');
define('DC_DBDRIVER', '');
define('DC_DBHOST', '');
define('DC_DBNAME', '');
define('DC_DBPASSWORD', '');
define('DC_DBPREFIX', '');
define('DC_DBUSER', '');
define('DC_DEBUG', false);
define('DC_DEFAULT_JQUERY', '');
define('DC_DEFAULT_TPLSET', '');
define('DC_DIGESTS', '');
define('DC_DISTRIB_PLUGINS', '');
define('DC_DISTRIB_THEMES', '');
define('DC_FAIRTRACKBACKS_FORCE', true);
define('DC_L10N_ROOT', '');
define('DC_L10N_UPDATE_URL', '');
define('DC_MASTER_KEY', '');
define('DC_MAX_UPLOAD_SIZE', 42);
define('DC_NEXT_REQUIRED_PHP', '');
define('DC_NOT_UPDATE', false);
define('DC_PLUGINS_ROOT', '');
define('DC_QUERY_TIMEOUT', 4);
define('DC_RC_PATH', '');
define('DC_ROOT', '');
define('DC_SESSION_NAME', '');
define('DC_SESSION_TTL', null);
define('DC_STORE_NOT_UPDATE', false);
define('DC_TPL_CACHE', '');
define('DC_UPDATE_URL', '');
define('DC_UPDATE_VERSION', '');
define('DC_VAR', '');
define('DC_VENDOR_NAME', '');
define('DC_VERSION', '');
define('DC_XMLRPC_URL', '');

BIN
inc/libs/phpstan.phar Normal file

Binary file not shown.