diff --git a/inc/lib.improve.action.phpstan.js b/inc/lib.improve.action.phpstan.js new file mode 100644 index 0000000..cb73437 --- /dev/null +++ b/inc/lib.improve.action.phpstan.js @@ -0,0 +1,7 @@ +/*global CodeMirror, dotclear */ +'use strict'; + +window.CodeMirror.defineMode('dotclear', function (config) { + config.readOnly = true; + return CodeMirror.getMode(config, 'text/x-yaml'); +}); \ No newline at end of file diff --git a/inc/lib.improve.action.phpstan.php b/inc/lib.improve.action.phpstan.php index f069aa4..b0dab98 100644 --- a/inc/lib.improve.action.phpstan.php +++ b/inc/lib.improve.action.phpstan.php @@ -12,6 +12,12 @@ */ class ImproveActionPhpstan extends ImproveAction { + /** @var boolean User pref to use colored synthax */ + protected static $user_ui_colorsyntax = false; + + /** @var string User pref for colored synthax theme */ + protected static $user_ui_colorsyntax_theme = 'default'; + protected function init(): bool { $this->setProperties([ @@ -23,6 +29,10 @@ class ImproveActionPhpstan extends ImproveAction 'types' => ['plugin'] ]); + $this->core->auth->user_prefs->addWorkspace('interface'); + self::$user_ui_colorsyntax = $this->core->auth->user_prefs->interface->colorsyntax; + self::$user_ui_colorsyntax_theme = $this->core->auth->user_prefs->interface->colorsyntax_theme; + return true; } @@ -31,19 +41,30 @@ class ImproveActionPhpstan extends ImproveAction return true; } + public function header(): ?string + { + if (self::$user_ui_colorsyntax) { + return dcPage::jsLoadCodeMirror(self::$user_ui_colorsyntax_theme); + } + + return null; + } + 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'], + 'phpexe_path' => (!empty($_POST['phpexe_path']) ? $_POST['phpexe_path'] : ''), + 'run_level' => (int) $_POST['run_level'], 'ignored_vars' => (!empty($_POST['ignored_vars']) ? $_POST['ignored_vars'] : ''), 'split_report' => !empty($_POST['split_report']) ]); $this->redirect($url); } + $content = (string) file_get_contents(dirname(__FILE__) . '/libs/dc.phpstan.rules.conf'); return + '
' . __('You must enable improve details to view analyse results !') . '
' . '' . @@ -52,20 +73,31 @@ class ImproveActionPhpstan extends ImproveAction __('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')]) . '
' . + form::number('run_level', ['min' => 0, 'max' => 9, 'default' => (int) $this->getSetting('run_level')]) . '' . '' . '
' . '' . sprintf(
- __('If you have errors like "%s", you can add this var here. Use ; as separator and do not put $ ahead.'),
+ __('If you have errors like "%s", you can add this var here. Use ; as separator and do not put $ ahead.'),
'Variable $var might not be defined'
) . ' ' . __('For exemple: var;_othervar;avar') . '
' . __('Some variables like core, _menu, are already set in ignored list.') . '
' . __('Enable this can cause timeout.') . '
' . - '' . __('You must enable improve details to view analyse results !') . '
'; + + '' . + '' . form::textarea('file_content', 120, 14, [ + 'default' => html::escapeHTML($content), + 'class' => 'maximal', + 'extra_html' => 'readonly="true"' + ]) . '
' . + ( + !self::$user_ui_colorsyntax ? '' : + dcPage::jsLoad(dcPage::getPF('improve/inc/lib.improve.action.phpstan.js')) . + dcPage::jsRunCodeMirror('editor', 'file_content', 'dotclear', self::$user_ui_colorsyntax_theme) + ); } public function openModule(): bool @@ -81,7 +113,7 @@ class ImproveActionPhpstan extends ImproveAction public function closeFile(): ?bool { - if (!$this->getSetting('split_report') + if (!$this->getSetting('split_report') || !in_array($this->path_extension, ['php', 'in']) ) { return null; @@ -151,36 +183,34 @@ class ImproveActionPhpstan extends ImproveAction private function writeConf(): bool { - $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"; - - // common - $content .= file_get_contents(dirname(__FILE__) . "/libs/dc.phpstan.neon.conf"); + $content = str_replace( + [ + '%LEVEL%', + '%MODULE_ROOT%', + '%DC_ROOT%', + '%BOOTSTRAP_ROOT%' + ], + [ + (int) $this->getSetting('run_level'), + $this->module['sroot'], + DC_ROOT, + dirname(__FILE__) . '/libs/' + ], + (string) file_get_contents(dirname(__FILE__) . '/libs/dc.phpstan.rules.conf') + ); $ignored = explode(';', $this->getSetting('ignored_vars')); - foreach($ignored as $var) { + foreach ($ignored as $var) { $var = trim($var); if (empty($var)) { continue; } - $content .= - ' # $' . $var .' variable may not be defined (globally)' . "\n" . + $content .= ' # $' . $var . ' variable may not be defined (globally)' . "\n" . ' - message: \'#Variable \$' . $var . ' might not be defined#\'' . "\n" . ' path: *' . "\n\n"; - } + } - return (boolean) file_put_contents(DC_VAR . '/phpstan.neon', $content); + return (bool) file_put_contents(DC_VAR . '/phpstan.neon', $content); } } diff --git a/inc/libs/dc.phpstan.neon.conf b/inc/libs/dc.phpstan.rules.conf similarity index 87% rename from inc/libs/dc.phpstan.neon.conf rename to inc/libs/dc.phpstan.rules.conf index 00b1ebc..75b95f3 100644 --- a/inc/libs/dc.phpstan.neon.conf +++ b/inc/libs/dc.phpstan.rules.conf @@ -1,3 +1,21 @@ +parameters: + level: %LEVEL% + + paths: + - %MODULE_ROOT% + + scanFiles: + - %DC_ROOT%/index.php + + scanDirectories: + - %DC_ROOT% + + excludePaths: + - %MODULE_ROOT%/*/libs/* + + bootstrapFiles: + - %BOOTSTRAP_ROOT%dc.phpstan.bootstrap.php + fileExtensions: - php - in @@ -35,6 +53,12 @@ - */_install.php - */_uninstall.php + # $list variable may not be defined (plugins/themes) + - message: '#Variable \$list might not be defined#' + paths: + - */_config.php + - */index.php + # $_menu variable may not be defined (plugins/themes) - message: '#Variable \$_menu might not be defined#' path: */_admin.php