diff --git a/_admin.php b/_admin.php index d789ea9..c89bfa1 100644 --- a/_admin.php +++ b/_admin.php @@ -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']); diff --git a/_prepend.php b/_prepend.php index d30d2aa..1a8ad7d 100644 --- a/_prepend.php +++ b/_prepend.php @@ -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' diff --git a/inc/lib.improve.action.phpstan.php b/inc/lib.improve.action.phpstan.php new file mode 100644 index 0000000..5b14193 --- /dev/null +++ b/inc/lib.improve.action.phpstan.php @@ -0,0 +1,131 @@ +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 + '
' . + '
' . + '' . + __('If this module does not work you can try to put here directory to php executable (without executable file name).') . + ' C:\path_to\php
' . + '' . + form::number('run_level', ['min' => 0, 'max' => 9, 'default' => (integer) $this->getSetting('run_level')]) . '
' . + '' . __('You must enable improve details to view analyse results !') . '
'; + } + + 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('%s', implode('