add module to use external php-cs-fixer

master
Jean-Christian Paul Denis 2021-11-01 22:08:56 +01:00
parent 68a4ac6b98
commit c0f9e027d5
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
5 changed files with 189 additions and 13 deletions

View File

@ -3,6 +3,7 @@ dev
- [ ] add module to check deprecated PHP function
- [ ] add module to check directory structure
- [ ] write documentation of php class
- add module to use external php-cs-fixer
0.3 - 2021.10.29
- use of xmlTag to generate dcstore.xml contents

View File

@ -1,16 +1,15 @@
<?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
*/
$core->blog->settings->addNamespace('improve');
$core->addBehavior('adminDashboardFavorites', ['ImproveBehaviors', 'adminDashboardFavorites']);
@ -22,6 +21,7 @@ $core->addBehavior('improveAddAction', ['ImproveActionGitshields', 'create']);
$core->addBehavior('improveAddAction', ['ImproveActionLicensefile', 'create']);
//$core->addBehavior('improveAddAction', ['ImproveActionLicense', 'create']);
$core->addBehavior('improveAddAction', ['ImproveActionNewline', 'create']);
$core->addBehavior('improveAddAction', ['ImproveActionPhpcsfixer', 'create']);
$core->addBehavior('improveAddAction', ['ImproveActionPhpheader', 'create']);
$core->addBehavior('improveAddAction', ['ImproveActionTab', 'create']);
$core->addBehavior('improveAddAction', ['ImproveActionZip', 'create']);
@ -49,4 +49,4 @@ class ImproveBehaviors
]
);
}
}
}

View File

@ -1,23 +1,22 @@
<?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
*/
if (!defined('DC_RC_PATH')) {
return;
}
$improve_libs = [
'Improve' => 'class.improve.php',
'ImproveAction' => 'class.improve.action.php',
'Improve' => 'class.improve.php',
'ImproveAction' => 'class.improve.action.php',
'ImproveActionDcdeprecated' => 'lib.improve.action.dcdeprecated.php',
'ImproveActionDcstore' => 'lib.improve.action.dcstore.php',
@ -25,11 +24,12 @@ $improve_libs = [
'ImproveActionGitshields' => 'lib.improve.action.gitshields.php',
'ImproveActionLicensefile' => 'lib.improve.action.licensefile.php',
'ImproveActionNewline' => 'lib.improve.action.php',
'ImproveActionPhpcsfixer' => 'lib.improve.action.phpcsfixer.php',
'ImproveActionPhpheader' => 'lib.improve.action.phpheader.php',
'ImproveActionTab' => 'lib.improve.action.php',
'ImproveActionZip' => 'lib.improve.action.zip.php',
'ImproveZipFileZip' => 'lib.improve.action.zip.php'
];
foreach($improve_libs as $class => $file) {
foreach ($improve_libs as $class => $file) {
$__autoload[$class] = dirname(__FILE__) . '/inc/' . $file;
}
}

View File

@ -0,0 +1,65 @@
<?php
// Adapted from https://gist.github.com/codfish/c77d348820c1c6b4ebe4a66dc2291c74
/**
* Rules we follow are from PSR-2 as well as the rectified PSR-2 guide.
*
* - https://github.com/FriendsOfPHP/PHP-CS-Fixer
* - https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md
* - https://github.com/php-fig-rectified/fig-rectified-standards/blob/master/PSR-2-R-coding-style-guide-additions.md
*
* If something isn't addressed in either of those, some other common community rules are
* used that might not be addressed explicitly in PSR-2 in order to improve code quality
* (so that devs don't need to comment on them in Code Reviews).
*
* For instance: removing trailing white space, removing extra line breaks where
* they're not needed (back to back, beginning or end of function/class, etc.),
* adding trailing commas in the last line of an array, etc.
*/
$finder = PhpCsFixer\Finder::create()
->exclude('node_modules')
->exclude('vendor')
->in(__DIR__);
$config = new PhpCsFixer\Config();
return $config
->setRules([
'@PSR2' => true,
'array_indentation' => true,
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => [
'default' => 'align_single_space_minimal',
'operators' => [
'=>' => 'align_single_space_minimal',
]
],
'blank_line_before_statement' => true,
'braces' => ['allow_single_line_closure' => true],
'cast_spaces' => true,
'combine_consecutive_unsets' => true,
'concat_space' => ['spacing' => 'one'],
'linebreak_after_opening_tag' => true,
'no_blank_lines_after_class_opening' => true,
'no_blank_lines_after_phpdoc' => true,
'no_break_comment' => false,
'no_extra_blank_lines' => true,
'no_trailing_comma_in_singleline_array' => true,
'no_whitespace_in_blank_line' => true,
'no_spaces_around_offset' => true,
'no_unused_imports' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'no_whitespace_before_comma_in_array' => true,
'normalize_index_brace' => true,
'phpdoc_indent' => true,
'phpdoc_to_comment' => true,
'phpdoc_trim' => true,
'return_type_declaration' => ['space_before' => 'none'],
'single_quote' => true,
'ternary_to_null_coalescing' => true,
'trailing_comma_in_multiline' => false,
'trim_array_spaces' => true,
])
->setFinder($finder);

View File

@ -0,0 +1,110 @@
<?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 ImproveActionPhpcsfixer extends ImproveAction
{
protected static $errors = [
0 => 'OK.',
1 => 'General error (or PHP minimal requirement not matched).',
4 => 'Some files have invalid syntax (only in dry-run mode).',
8 => 'Some files need fixing (only in dry-run mode).',
16 => 'Configuration error of the application.',
32 => 'Configuration error of a Fixer.',
64 => 'Exception raised within the application'
];
protected function init(): bool
{
$this->setProperties([
'id' => 'phpcsfixer',
'name' => __('PHP CS Fixer'),
'desc' => __('Fix PSR coding style using Php CS Fixer'),
'priority' => 920,
'config' => true,
'types' => ['plugin', 'theme']
]);
return true;
}
public function isConfigured(): bool
{
return !empty($this->getSetting('phpcsf_path'));
}
public function configure($url): ?string
{
if (!empty($_POST['save'])) {
$this->setSettings([
'phpexe_path' => !empty($_POST['phpexe_path']) ? $_POST['phpexe_path'] : '',
'phpcsf_path' => !empty($_POST['phpcsf_path']) ? $_POST['phpcsf_path'] : ''
]);
$this->redirect($url);
}
return
'<p class="info">' . sprintf(
__('You must have installed %s to use this tool'),
'<a href="https://github.com/FriendsOfPHP/PHP-CS-Fixer">php-cs-fixer</a>'
) . '</p>' .
'<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 server is under unix, leave it empty.') . ' ' .
__('If this server is under Windows, put here directory to php executable (without executable file name).') .
' C:\path_to\php</p>' .
'<p><label class="classic" for="phpcsf_path">' .
__('Root directory to "friendsofphp php-cs-fixer":') . '<br />' .
form::field('phpcsf_path', 160, 255, $this->getSetting('phpcsf_path')) . '</label>' .
'</p>' .
'<p class="form-note">' . __('Do not add file name to the end of path.') . ' \path_to\tools\php-cs-fixer\vendor\friendsofphp\php-cs-fixer</p>';
}
public function closeModule(): ?bool
{
$phpexe_path = path::real($this->getSetting('phpexe_path'));
if (!empty($phpexe_path)) {
$phpexe_path .= '/';
}
$phpcsf_path = path::real($this->getSetting('phpcsf_path'));
$command = sprintf(
'%sphp %s/php-cs-fixer fix %s --config=%s/dc.phpcsfixer.rules.php',
$phpexe_path,
$phpcsf_path,
$this->module['sroot'],
dirname(__FILE__)
);
try {
exec($command, $output, $error);
if (empty($output)) {
if (isset(self::$errors[$error])) {
$this->setError(self::$errors[$error]);
return false;
}
throw new Exception('oops');
}
$this->setSuccess(sprintf('<pre>%s</pre>', implode('<br />', $output)));
return true;
} catch (Exception $e) {
$this->setError(__('Failed to run php-cs-fixer'));
return false;
}
}
}