From 102c4e128a3239d5ad2fc461e3dd1054ff656919 Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Mon, 1 Nov 2021 22:17:44 +0100 Subject: [PATCH] test php-cs-fixer module on itself --- _define.php | 9 +- inc/class.improve.action.php | 126 ++++++++++++------------ inc/class.improve.php | 80 ++++++++------- inc/lib.improve.action.dcdeprecated.php | 19 ++-- inc/lib.improve.action.dcstore.php | 34 +++---- inc/lib.improve.action.gitshields.php | 53 +++++----- inc/lib.improve.action.licensefile.php | 28 +++--- inc/lib.improve.action.php | 87 ++++++++-------- inc/lib.improve.action.phpheader.php | 37 +++---- inc/lib.improve.action.zip.php | 49 ++++----- index.php | 89 ++++++++--------- 11 files changed, 314 insertions(+), 297 deletions(-) diff --git a/_define.php b/_define.php index 31dcbd6..93bbdbb 100644 --- a/_define.php +++ b/_define.php @@ -1,16 +1,15 @@ registerModule( 'details' => 'https://github.com/JcDenis/improve', 'repository' => 'https://raw.githubusercontent.com/JcDenis/improve/master/dcstore.xml' ] -); \ No newline at end of file +); diff --git a/inc/class.improve.action.php b/inc/class.improve.action.php index 7cf65b8..523e358 100644 --- a/inc/class.improve.action.php +++ b/inc/class.improve.action.php @@ -1,12 +1,12 @@ addBehavior('ImproveAddAction'), ['myClass', 'create']); - * yoru action class is automatically created, + * yoru action class is automatically created, * then function init() of your class wil be called. * One class must manage only one action. * @@ -30,38 +30,38 @@ abstract class ImproveAction { protected $core; - protected $module = []; - protected $path_full = ''; + protected $module = []; + protected $path_full = ''; protected $path_extension = ''; - protected $path_is_dir = null; + protected $path_is_dir = null; - private $logs = ['success' => [], 'warning' => [], 'error' => []]; - private $settings = []; + private $logs = ['success' => [], 'warning' => [], 'error' => []]; + private $settings = []; private $properties = [ - 'id' => '', - 'name' => '', - 'desc' => '', + 'id' => '', + 'name' => '', + 'desc' => '', 'priority' => 500, - 'config' => false, //mixed bool for internal, string for ext url - 'types' => ['plugin'] + 'config' => false, //mixed bool for internal, string for ext url + 'types' => ['plugin'] ]; /** * ImproveAction constructor inits properpties and settings of a child class. - * + * * @param string $core dcCore instance */ final public function __construct(dcCore $core) { $this->core = $core; - $settings = @unserialize($core->blog->settings->improve->get('settings_' . get_called_class())); + $settings = @unserialize($core->blog->settings->improve->get('settings_' . get_called_class())); $this->settings = is_array($settings) ? $settings : []; $this->init(); // can overload priority by settings - if (1 < ($p = (int) $core->blog->settings->improve->get('priority_'. get_called_class()))) { + if (1 < ($p = (int) $core->blog->settings->improve->get('priority_' . get_called_class()))) { $this->priority = $p; } } @@ -80,10 +80,10 @@ abstract class ImproveAction /** * Action initialisation function. - * - * It's called when an instance of ImproveAction child class is created. + * + * It's called when an instance of ImproveAction child class is created. * Usefull to setup action class. - * + * * @return bool True if initialisation is ok. */ abstract protected function init(): bool; @@ -100,7 +100,7 @@ abstract class ImproveAction /** * Get a definition property of action class - * + * * @return mixed A property of action definition. */ final public function getProperty(string $property) @@ -110,7 +110,7 @@ abstract class ImproveAction /** * Set a definition property of action class - * + * * Property can be: * - id : action id * - name : action short name @@ -118,16 +118,16 @@ abstract class ImproveAction * - priority : order of execution of this action * - config : as configuration gui, false = none, true = internal, string = ext url * - types : array of supported type of module, can : be plugins and/or themes - * + * * @param mixed $property one or more definition * @param dtring $value value for a single property - * + * * @return mixed A property of action definition. */ final protected function setProperties($property, $value = null): bool { $properties = is_array($property) ? $property : [$property => $value]; - foreach($properties as $k => $v) { + foreach ($properties as $k => $v) { if (isset($this->properties[$k])) { if ($k == 'types' && !is_array($v)) { $v = [$v]; @@ -135,6 +135,7 @@ abstract class ImproveAction $this->properties[$k] = $v; } } + return true; } //@} @@ -143,9 +144,9 @@ abstract class ImproveAction //@{ /** * Get a settings of action class - * + * * @param string $setting a settings id - * + * * @return mixed A setting of action. */ final protected function getSetting(string $setting) @@ -155,36 +156,37 @@ abstract class ImproveAction /** * Set one or more setting of action class - * + * * @param mixed $settings one or more settings * @param string $value value for a single setting - * + * * @return mixed A setting of action. */ final protected function setSettings($settings, $value = null) { $settings = is_array($settings) ? $settings : [$settings => $value]; - foreach($settings as $k => $v) { + foreach ($settings as $k => $v) { $this->settings[$k] = $v; } + return true; } /** * Redirection after settings update - * + * * This save settings update before redirect. - * + * * @param string $url redirect url after settings update */ final protected function redirect(string $url) { $this->core->blog->settings->improve->put( - 'settings_' . get_called_class(), - serialize($this->settings), - 'string', - null, - true, + 'settings_' . get_called_class(), + serialize($this->settings), + 'string', + null, + true, true ); $this->core->blog->triggerBlog(); @@ -194,22 +196,22 @@ abstract class ImproveAction /** * Check if action class is well configured - * + * * @return boolean True if class action is well configured */ abstract public function isConfigured(): bool; /** * Get configuraton gui - * - * If action class uses internal configuration, + * + * If action class uses internal configuration, * it must share here html form content of its settings. * It must not use enclose bloc "form" nor button "save". - * This function is also called to redirect form + * This function is also called to redirect form * after validation with $this->redirect($url); - * + * * @param string $url post form redirect url - * + * * @return mixed A setting of action. */ public function configure(string $url): ?string @@ -220,9 +222,9 @@ abstract class ImproveAction /** * Set in class var current module definitions. - * + * * @see Improve::sanitizeModule() - * + * * @param array $module Full array of module definitons */ final public function setModule(array $module) @@ -232,16 +234,16 @@ abstract class ImproveAction /** * Set in class var current path definitons. - * + * * @param string $path_full Full path * @param string $path_extension Path extension (if it is a file) * @param string $path_is_dir True if path is a directory */ final public function setPath(string $path_full, string $path_extension, bool $path_is_dir) { - $this->path_full = $path_full; + $this->path_full = $path_full; $this->path_extension = $path_extension; - $this->path_is_dir = $path_is_dir; + $this->path_is_dir = $path_is_dir; } /// @name Fix methods @@ -272,12 +274,12 @@ abstract class ImproveAction /** * Called when read content of a file to fix. - * + * * Content is shared from action to another. * If an action erase content, fix is stopped. - * If you want to erase a content you must erase + * If you want to erase a content you must erase * the file on action openDirectory. - * + * * @param string $content File content */ public function readFile(string &$content): ?bool @@ -306,12 +308,12 @@ abstract class ImproveAction //@{ /** * Set an action log. - * + * * Log must be use every time an action something happen. - * + * * @param string $type type of message, can be error, warning, succes * @param string $message message to log - * + * * @return boolean True if message is logged. */ final public function setLog(string $type, string $message): bool @@ -320,14 +322,15 @@ abstract class ImproveAction return false; } $this->logs[$type][$this->path_full][] = $message; + return true; } /** * Check if action class has log of given type. - * + * * @param string $type type of message, can be error, warning, succes - * + * * @return boolean True if messages exist. */ final public function hasLog(string $type): bool @@ -337,9 +340,9 @@ abstract class ImproveAction /** * Get action logs. - * + * * @param mixed $type type of message, can be error, warning, succes - * + * * @return array Arry of given type of log or all if type is null */ final public function getLogs($type = null): array @@ -347,12 +350,13 @@ abstract class ImproveAction if (null === $type) { return $this->logs; } - if (empty($this->path_full) + if (empty($this->path_full) || !array_key_exists($type, $this->logs) || !array_key_exists($this->path_full, $this->logs[$type]) ) { return []; } + return $this->logs[$type][$this->path_full]; } @@ -428,4 +432,4 @@ abstract class ImproveAction return $this->getLogs('success'); } //@} -} \ No newline at end of file +} diff --git a/inc/class.improve.php b/inc/class.improve.php index 612c8bb..bcab46b 100644 --- a/inc/class.improve.php +++ b/inc/class.improve.php @@ -1,12 +1,12 @@ false, 'warning' => false, 'error' => false]; public function __construct(dcCore $core) @@ -33,8 +33,8 @@ class Improve try { $this->core->callBehavior('improveAddAction', $list, $this->core); - foreach($list as $action) { - if ($action instanceOf ImproveAction && !isset($this->actions[$action->id])) { + foreach ($list as $action) { + if ($action instanceof ImproveAction && !isset($this->actions[$action->id])) { $this->actions[$action->id] = $action; } } @@ -59,21 +59,22 @@ class Improve if (empty($this->logs)) { return ''; } - $cur = $this->core->con->openCursor($this->core->prefix . 'log'); - $cur->log_msg = serialize($this->logs); + $cur = $this->core->con->openCursor($this->core->prefix . 'log'); + $cur->log_msg = serialize($this->logs); $cur->log_table = 'improve'; - $id = $this->core->log->addLog($cur); + $id = $this->core->log->addLog($cur); return $id; } - public function readLogs(int $id ): array + public function readLogs(int $id): array { $rs = $this->core->log->getLogs(['log_table' => 'improve', 'log_id' => $id, 'limit' => 1]); if ($rs->isEmpty()) { return []; } $this->core->log->delLogs($rs->log_id); + return unserialize($rs->log_msg); } @@ -84,14 +85,14 @@ class Improve return []; } $lines = []; - foreach($logs['improve'] as $path => $tools) { + foreach ($logs['improve'] as $path => $tools) { $l_types = []; - foreach(['success', 'warning', 'error'] as $type) { + foreach (['success', 'warning', 'error'] as $type) { $l_tools = []; - foreach($tools as $tool) { + foreach ($tools as $tool) { $l_msg = []; if (!empty($logs[$tool][$type][$path])) { - foreach($logs[$tool][$type][$path] as $msg) { + foreach ($logs[$tool][$type][$path] as $msg) { $l_msg[] = $msg; } } @@ -107,6 +108,7 @@ class Improve $lines[$path] = $l_types; } } + return $lines; } @@ -115,6 +117,7 @@ class Improve if (empty($id)) { return $this->actions; } + return $this->actions[$id] ?? null; } @@ -123,21 +126,22 @@ class Improve if (empty($id)) { return $this->actions; } + return $this->actions[$id] ?? null; } public function fixModule(string $type, string $id, array $properties, array $actions): float { $time_start = microtime(true); - $module = ImproveDefinition::clean($type, $id, $properties); + $module = ImproveDefinition::clean($type, $id, $properties); $workers = []; - foreach($actions as $action) { + foreach ($actions as $action) { if (isset($this->actions[$action]) && $this->actions[$action]->isConfigured()) { $workers[] = $this->actions[$action]; } } - foreach($workers as $action) { + foreach ($workers as $action) { // trace all path and action in logs $this->logs['improve'][__('Begin')][] = $action->id; // info: set current module @@ -150,48 +154,50 @@ class Improve throw new Exception(__('Module path is not writable')); } $tree = self::getModuleFiles($module['sroot']); - foreach($tree as $file) { + foreach ($tree as $file) { if (!file_exists($file[0])) { continue; } - foreach($workers as $action) { + foreach ($workers as $action) { // trace all path and action in logs $this->logs['improve'][$file[0]][] = $action->id; // info: set current path $action->setPath($file[0], $file[1], $file[2]); } if (!$file[2]) { - foreach($workers as $action) { + foreach ($workers as $action) { // action: open a directory. full path $action->openDirectory(); } } else { - foreach($workers as $action) { + foreach ($workers as $action) { // action: before openning a file. full path, extension $action->openFile(); } if (in_array($file[1], self::$readfile_extensions)) { if (false !== ($content = file_get_contents($file[0]))) { $no_content = empty($content); - foreach($workers as $action) { + foreach ($workers as $action) { // action: read a file content. full path, extension, content $action->readFile($content); if (empty($content) && !$no_content) { throw new Exception(sprintf( - __('File content has been removed: %s by %s'), $file[0], $action->name + __('File content has been removed: %s by %s'), + $file[0], + $action->name )); } } files::putContent($file[0], $content); } - foreach($workers as $action) { + foreach ($workers as $action) { // action: after closing a file. full path, extension $action->closeFile(); } } } } - foreach($workers as $action) { + foreach ($workers as $action) { // trace all path and action in logs $this->logs['improve'][__('End')][] = $action->id; // info: set current module @@ -200,9 +206,9 @@ class Improve $action->closeModule(); } // info: get acions reports - foreach($workers as $action) { + foreach ($workers as $action) { $this->logs[$action->id] = $action->getLogs(); - foreach($this->has_log as $type => $v) { + foreach ($this->has_log as $type => $v) { if ($action->hasLog($type)) { $this->has_log[$type] = true; } @@ -224,19 +230,21 @@ class Improve $res[] = [$dir, '', false]; $files = files::scandir($path); - foreach($files AS $file) { + foreach ($files as $file) { if (substr($file, 0, 1) == '.') { continue; } if (is_dir($path . '/' . $file)) { $res = self::getModuleFiles( - $path . '/' . $file, $dir . '/' . $file, + $path . '/' . $file, + $dir . '/' . $file, $res ); } else { $res[] = [empty($dir) ? $file : $dir . '/' . $file, files::getExtension($file), true]; } } + return $res; } @@ -252,13 +260,14 @@ class Improve $in = explode(',', $in); } if (!empty($in)) { - foreach($in as $v) { + foreach ($in as $v) { $v = trim(files::getExtension('a.' . $v)); if (!empty($v)) { $out[] = $v; } } } + return $out; } @@ -267,6 +276,7 @@ class Improve if ($a->priority == $b->priority) { return strcasecmp($a->name, $b->name); } + return $a->priority < $b->priority ? -1 : 1; } } @@ -290,6 +300,7 @@ class ImproveDefinition public static function clean(string $type, string $id, array $properties): array { $p = new self($type, $id, $properties); + return $p->get(); } @@ -314,7 +325,7 @@ class ImproveDefinition $properties['permissions'] = $args[4]; } if (isset($args[5])) { - $properties['priority'] = (integer) $args[5]; + $properties['priority'] = (int) $args[5]; } } @@ -328,7 +339,8 @@ class ImproveDefinition 'requires' => [], 'settings' => [], 'repository' => '' - ], $properties + ], + $properties ); } @@ -337,7 +349,7 @@ class ImproveDefinition { $label = empty($properties['label']) ? $id : $properties['label']; $name = __(empty($properties['name']) ? $label : $properties['name']); - $oname = empty($properties['name']) ? $label : $properties['name']; + $oname = empty($properties['name']) ? $label : $properties['name']; return array_merge( # Default values @@ -385,4 +397,4 @@ class ImproveDefinition { return preg_replace('/[^A-Za-z0-9\@\#+_-]/', '', strtolower($str)); } -} \ No newline at end of file +} diff --git a/inc/lib.improve.action.dcdeprecated.php b/inc/lib.improve.action.dcdeprecated.php index 9352adf..866c73b 100644 --- a/inc/lib.improve.action.dcdeprecated.php +++ b/inc/lib.improve.action.dcdeprecated.php @@ -1,16 +1,15 @@ setProperties([ - 'id' => 'dcdeprecated', - 'name' => __('Dotclear deprecated'), - 'desc' => __('Search for use of deprecated Dotclear functions'), + 'id' => 'dcdeprecated', + 'name' => __('Dotclear deprecated'), + 'desc' => __('Search for use of deprecated Dotclear functions'), 'priority' => 520, - 'types' => ['plugin', 'theme'] + 'types' => ['plugin', 'theme'] ]); return true; @@ -83,7 +82,7 @@ class ImproveActionDcdeprecated extends ImproveAction if (!in_array($this->path_extension, array_keys(self::$deprecated))) { return null; } - foreach(self::$deprecated[$this->path_extension] as $d) { + foreach (self::$deprecated[$this->path_extension] as $d) { if (preg_match('/' . $d[0] . '/i', $content)) { $this->setWarning(sprintf(__('Use of deprecated "%s", you should use "%s" instead.'), $d[1], __($d[2]))); } @@ -91,4 +90,4 @@ class ImproveActionDcdeprecated extends ImproveAction return true; } -} \ No newline at end of file +} diff --git a/inc/lib.improve.action.dcstore.php b/inc/lib.improve.action.dcstore.php index 07e6760..9f2b094 100644 --- a/inc/lib.improve.action.dcstore.php +++ b/inc/lib.improve.action.dcstore.php @@ -1,27 +1,26 @@ setProperties([ - 'id' => 'dcstore', - 'name' => __('Store file'), - 'desc' => __('Re-create dcstore.xml file according to _define.php variables'), + 'id' => 'dcstore', + 'name' => __('Store file'), + 'desc' => __('Re-create dcstore.xml file according to _define.php variables'), 'priority' => 420, - 'config' => true, - 'types' => ['plugin', 'theme'] + 'config' => true, + 'types' => ['plugin', 'theme'] ]); return true; @@ -39,16 +38,16 @@ class ImproveActionDcstore extends ImproveAction $this->redirect($url); } - return + return '

' . __('File will be overwritten if it exists') . '

' . - '

' . '

' . - '

' . - sprintf(__('You can use wildcards %s'), '%author%, %type%, %id%, %version%.') . + '

' . + sprintf(__('You can use wildcards %s'), '%author%, %type%, %id%, %version%.') . '
' . - __('For exemple on github https://github.com/MyGitName/%id%/releases/download/v%version%/%type%-%id%.zip') . + __('For exemple on github https://github.com/MyGitName/%id%/releases/download/v%version%/%type%-%id%.zip') . '
' . __('Note on github, you must create a release and join to it the module zip file.') . '

'; @@ -60,10 +59,11 @@ class ImproveActionDcstore extends ImproveAction if ($this->hasError()) { return false; } + try { files::putContent($this->module['sroot'] . '/dcstore.xml', $content); $this->setSuccess(__('Write dcstore.xml file.')); - } catch(Exception $e) { + } catch (Exception $e) { $this->setError(__('Failed to write dcstore.xml file')); return false; @@ -98,7 +98,6 @@ class ImproveActionDcstore extends ImproveAction # author if (empty($this->module['author'])) { $this->setError(__('unknow module author')); - } $rsp->author($this->module['author']); @@ -128,6 +127,7 @@ class ImproveActionDcstore extends ImproveAction } if ($req[0] == 'core') { $this->module['dc_min'] = $req[1]; + break; } } @@ -187,4 +187,4 @@ class ImproveActionDcstore extends ImproveAction $this->getSetting('pattern') )); } -} \ No newline at end of file +} diff --git a/inc/lib.improve.action.gitshields.php b/inc/lib.improve.action.gitshields.php index 664806b..e5ee56b 100644 --- a/inc/lib.improve.action.gitshields.php +++ b/inc/lib.improve.action.gitshields.php @@ -1,30 +1,29 @@ '/\[!\[Release(.*)LICENSE\)/ms', 'target' => '/^([^\n]+)[\r\n|\n]{1,}/ms' ]; protected $bloc_content = [ - 'release' => '[![Release](https://img.shields.io/github/v/release/%username%/%module%)](https://github.com/%username%/%module%/releases)', - 'date' => '[![Date](https://img.shields.io/github/release-date/%username%/%module%)](https://github.com/%username%/%module%/releases)', - 'issues' => '[![Issues](https://img.shields.io/github/issues/%username%/%module%)](https://github.com/%username%/%module%/issues)', - 'dotclear' => '[![Dotclear](https://img.shields.io/badge/dotclear-v%dotclear%-blue.svg)](https://fr.dotclear.org/download)', + 'release' => '[![Release](https://img.shields.io/github/v/release/%username%/%module%)](https://github.com/%username%/%module%/releases)', + 'date' => '[![Date](https://img.shields.io/github/release-date/%username%/%module%)](https://github.com/%username%/%module%/releases)', + 'issues' => '[![Issues](https://img.shields.io/github/issues/%username%/%module%)](https://github.com/%username%/%module%/issues)', + 'dotclear' => '[![Dotclear](https://img.shields.io/badge/dotclear-v%dotclear%-blue.svg)](https://fr.dotclear.org/download)', 'dotaddict' => '[![Dotaddict](https://img.shields.io/badge/dotaddict-official-green.svg)](https://%type%s.dotaddict.org/dc2/details/%module%)', - 'license' => '[![License](https://img.shields.io/github/license/%username%/%module%)](https://github.com/%username%/%module%/blob/master/LICENSE)' + 'license' => '[![License](https://img.shields.io/github/license/%username%/%module%)](https://github.com/%username%/%module%/blob/master/LICENSE)' ]; protected function init(): bool @@ -50,7 +49,7 @@ class ImproveActionGitshields extends ImproveAction { if (!empty($_POST['save']) && !empty($_POST['username'])) { $this->setSettings([ - 'username' => (string) $_POST['username'], + 'username' => (string) $_POST['username'], 'dotaddict' => !empty($_POST['dotaddict']) ]); $this->redirect($url); @@ -62,8 +61,8 @@ class ImproveActionGitshields extends ImproveAction

' . __('Used in your Github URL: http://github.com/username/module_id.') . '
' . __('If you have badges not created by this tool in the README.md file you should remove them manually.') . '

-

' . __('If your plugin or theme is on Dotaddict, you can add a badge to link to its details in Dotaddict.') . '

'; } @@ -81,8 +80,8 @@ class ImproveActionGitshields extends ImproveAction return null; } - $clean = $this->deleteShieldsBloc($content); - $content = $this->writeShieldsBloc($clean); + $clean = $this->deleteShieldsBloc($content); + $content = $this->writeShieldsBloc($clean); $this->stop_scan = true; return true; @@ -91,23 +90,23 @@ class ImproveActionGitshields extends ImproveAction private function replaceInfo() { $bloc = []; - foreach($this->bloc_content as $k => $v) { + foreach ($this->bloc_content as $k => $v) { if ($k == 'dotaddict' && empty($this->getSetting('dotaddict'))) { continue; } $bloc[$k] = trim(str_replace( [ - '%username%', - '%module%', - '%dotclear%', - '%type%', + '%username%', + '%module%', + '%dotclear%', + '%type%', "\r\n", "\n" ], [ - $this->getSetting('username'), - $this->module['id'], - $dotclear = $this->getDotclearVersion(), - $this->module['type'], + $this->getSetting('username'), + $this->module['id'], + $dotclear = $this->getDotclearVersion(), + $this->module['type'], '', '' ], $v @@ -127,12 +126,14 @@ class ImproveActionGitshields extends ImproveAction } if ($req[0] == 'core') { $version = $req[1]; + break; } } } elseif (!empty($this->module['dc_min'])) { $version = $this->module['dc_min']; } + return $version ?: $this->core->getVersion('core'); } @@ -148,6 +149,7 @@ class ImproveActionGitshields extends ImproveAction if ($count) { $this->setSuccess(__('Write new shield bloc')); } + return $res; } @@ -163,6 +165,7 @@ class ImproveActionGitshields extends ImproveAction if ($count) { $this->setSuccess(__('Delete old shield bloc')); } + return $res; } -} \ No newline at end of file +} diff --git a/inc/lib.improve.action.licensefile.php b/inc/lib.improve.action.licensefile.php index 01272c8..08eb68f 100644 --- a/inc/lib.improve.action.licensefile.php +++ b/inc/lib.improve.action.licensefile.php @@ -1,16 +1,15 @@ 'full', __('Remove license files') => 'remove' ]; + return true; } @@ -58,8 +58,8 @@ class ImproveActionLicensefile extends ImproveAction { if (!empty($_POST['save'])) { $this->setSettings([ - 'action_version' => !empty($_POST['action_version']) ? $_POST['action_version'] : '', - 'action_full' => !empty($_POST['action_full']) ? $_POST['action_full'] : '' + 'action_version' => !empty($_POST['action_version']) ? $_POST['action_version'] : '', + 'action_full' => !empty($_POST['action_full']) ? $_POST['action_full'] : '' ]); $this->redirect($url); } @@ -70,7 +70,7 @@ class ImproveActionLicensefile extends ImproveAction

' . - form::combo('action_full', $this->action_full, $this->getSetting('action_full')) . + form::combo('action_full', $this->action_full, $this->getSetting('action_full')) . '

'; } @@ -86,6 +86,7 @@ class ImproveActionLicensefile extends ImproveAction $this->writeFullLicense(); } } + return null; } @@ -105,12 +106,13 @@ class ImproveActionLicensefile extends ImproveAction return null; } + return true; } private function deleteFullLicense($only_one = false) { - foreach(self::fileExists($this->module['root']) as $file) { + foreach (self::fileExists($this->module['root']) as $file) { if ($only_one && $file != 'LICENSE') { continue; } @@ -122,17 +124,19 @@ class ImproveActionLicensefile extends ImproveAction $this->setSuccess(sprintf(__('Delete old license file "%s"'), $file)); } } + return true; } private static function fileExists($root) { $existing = []; - foreach(self::$license_filenames as $file) { + foreach (self::$license_filenames as $file) { if (file_exists($root . '/' . strtolower($file))) { $existing[] = strtolower($file); } } + return $existing; } -} \ No newline at end of file +} diff --git a/inc/lib.improve.action.php b/inc/lib.improve.action.php index 35f179c..1e6733c 100644 --- a/inc/lib.improve.action.php +++ b/inc/lib.improve.action.php @@ -1,26 +1,25 @@ setProperties([ - 'id' => 'tab', - 'name' => __('Tabulations'), - 'desc' => __('Replace tabulation by four space in php files'), + 'id' => 'tab', + 'name' => __('Tabulations'), + 'desc' => __('Replace tabulation by four space in php files'), 'priority' => 820, - 'types' => ['plugin', 'theme'] + 'types' => ['plugin', 'theme'] ]); return true; @@ -53,20 +52,20 @@ class ImproveActionNewline extends ImproveAction protected function init(): bool { $this->setProperties([ - 'id' => 'newline', - 'name' => __('Newlines'), - 'desc' => __('Replace bad and repetitive and empty newline by single newline in files'), + 'id' => 'newline', + 'name' => __('Newlines'), + 'desc' => __('Replace bad and repetitive and empty newline by single newline in files'), 'priority' => 840, - 'config' => true, - 'types' => ['plugin', 'theme'] + 'config' => true, + 'types' => ['plugin', 'theme'] ]); -/* - $ext = @unserialize($this->core->blog->settings->improve->newline_extensions); - $ext = Improve::cleanExtensions($ext); - if (!empty($ext)) { - $this->extensions = $ext; - } -*/ + /* + $ext = @unserialize($this->core->blog->settings->improve->newline_extensions); + $ext = Improve::cleanExtensions($ext); + if (!empty($ext)) { + $this->extensions = $ext; + } + */ return true; } @@ -79,7 +78,7 @@ class ImproveActionNewline extends ImproveAction { if (!empty($_POST['save']) && !empty($_POST['newline_extensions'])) { $this->setSettings( - 'extensions', + 'extensions', Improve::cleanExtensions($_POST['newline_extensions']) ); $this->redirect($url); @@ -90,12 +89,12 @@ class ImproveActionNewline extends ImproveAction $ext = []; } - return - '

' . - __('Use comma separated list of extensions without dot, recommand "php,js,xml,txt,md".') . + __('Use comma separated list of extensions without dot, recommand "php,js,xml,txt,md".') . '

'; } @@ -106,14 +105,14 @@ class ImproveActionNewline extends ImproveAction return null; } $clean = preg_replace( - '/(\n\s+\n)/', - "\n\n", + '/(\n\s+\n)/', + "\n\n", preg_replace( - '/(\n\n+)/', - "\n\n", + '/(\n\n+)/', + "\n\n", str_replace( - ["\r\n", "\r"], - "\n", + ["\r\n", "\r"], + "\n", $content ) ) @@ -134,12 +133,12 @@ class ImproveActionEndoffile extends ImproveAction protected function init(): bool { $this->setProperties([ - 'id' => 'endoffile', - 'name' => __('End of files'), - 'desc' => __('Remove php tag and empty lines from end of files'), + 'id' => 'endoffile', + 'name' => __('End of files'), + 'desc' => __('Remove php tag and empty lines from end of files'), 'priority' => 860, - 'config' => true, - 'types' => ['plugin', 'theme'] + 'config' => true, + 'types' => ['plugin', 'theme'] ]); return true; @@ -157,12 +156,12 @@ class ImproveActionEndoffile extends ImproveAction $this->redirect($url); } - return + return '

' . - __('PSR2 must have a blank line, whereas PSR12 must not.') . + form::checkbox('endoffile_psr2', 255, $this->getSetting('psr2')) . + __('Add a blank line to the end of file') . + '

' . + __('PSR2 must have a blank line, whereas PSR12 must not.') . '

'; } @@ -172,8 +171,8 @@ class ImproveActionEndoffile extends ImproveAction return null; } $clean = preg_replace( - ['/(\s*)(\?>\s*)$/', '/\n+$/'], - '', + ['/(\s*)(\?>\s*)$/', '/\n+$/'], + '', $content ) . ($this->getSetting('psr2') ? "\n" : ''); if ($content != $clean) { @@ -183,4 +182,4 @@ class ImproveActionEndoffile extends ImproveAction return true; } -} \ No newline at end of file +} diff --git a/inc/lib.improve.action.phpheader.php b/inc/lib.improve.action.phpheader.php index 232224d..e70dd2e 100644 --- a/inc/lib.improve.action.phpheader.php +++ b/inc/lib.improve.action.phpheader.php @@ -1,16 +1,15 @@ action_bloc = [ __('Do nothing') => 0, __('Add bloc if it does not exist') => 'create', - __('Add and overwrite bloc') => 'overwrite' , + __('Add and overwrite bloc') => 'overwrite', __('Overwrite bloc only if it exists') => 'replace', __('Remove existing bloc header') => 'remove' ]; @@ -97,12 +96,12 @@ class ImproveActionPhpheader extends ImproveAction

' . __('Bloc content:') . '

' . form::textarea('bloc_content', 50, 10, html::escapeHTML($this->getSetting('bloc_content'))) . ' -

' . +

' . sprintf( - __('You can use wildcards %s') , + __('You can use wildcards %s'), '%year%, %module_id%, %module_name%, %module_author%, %module_type%, %user_cn%, %user_name%, %user_email%, %user_url%' ) . '
' . __('Do not put structural elements to the begining of lines.') . '

' . - '

' . __('Exemple') .'

' . self::$exemple . '
'; + '

' . __('Exemple') . '

' . self::$exemple . '
'; } public function openModule(): ?bool @@ -114,7 +113,7 @@ class ImproveActionPhpheader extends ImproveAction public function openDirectory(): ?bool { - $skipped = $this->stop_scan; + $skipped = $this->stop_scan; $this->stop_scan = false; if (!empty($this->getSetting('exclude_locales')) && preg_match('/\/(locales|libs)(\/.*?|)$/', $this->path_full)) { if (!$skipped) { @@ -128,7 +127,7 @@ class ImproveActionPhpheader extends ImproveAction public function readFile(&$content): ?bool { - if ($this->stop_scan || $this->path_extension !='php' || $this->hasError()) { + if ($this->stop_scan || $this->path_extension != 'php' || $this->hasError()) { return null; } @@ -136,7 +135,6 @@ class ImproveActionPhpheader extends ImproveAction $content = $this->deleteOldBloc($content); } if (empty($this->getSetting('bloc_action'))) { - return null; } $clean = $this->deleteDocBloc($content); @@ -146,11 +144,9 @@ class ImproveActionPhpheader extends ImproveAction return null; } if ($content != $clean && $this->getSetting('bloc_action') == 'create') { - return null; } if ($content == $clean && $this->getSetting('bloc_action') == 'replace') { - return null; } @@ -174,8 +170,10 @@ class ImproveActionPhpheader extends ImproveAction try { $this->bloc = preg_replace_callback( // use \u in bloc content for first_upper_case - '/(\\\u([a-z]{1}))/', - function($str) { return ucfirst($str[2]); }, + '/(\\\u([a-z]{1}))/', + function ($str) { + return ucfirst($str[2]); + }, str_replace( $this->bloc_wildcards, [ @@ -213,6 +211,7 @@ class ImproveActionPhpheader extends ImproveAction $res = str_replace("\n * \n", "\n *\n", $res); $this->setSuccess(__('Write new doc bloc content')); } + return $res; } @@ -228,6 +227,7 @@ class ImproveActionPhpheader extends ImproveAction if ($count) { $this->setSuccess(__('Delete old doc bloc content')); } + return $res; } @@ -235,7 +235,7 @@ class ImproveActionPhpheader extends ImproveAction { $res = preg_replace( '/((# -- BEGIN LICENSE BLOCK ([-]+))(.*?)(# -- END LICENSE BLOCK ([-]+))([\n|\r\n]{1,}))/msi', - "", + '', $content, -1, $count @@ -243,6 +243,7 @@ class ImproveActionPhpheader extends ImproveAction if ($count) { $this->setSuccess(__('Delete old style bloc content')); } + return $res; } -} \ No newline at end of file +} diff --git a/inc/lib.improve.action.zip.php b/inc/lib.improve.action.zip.php index 2cfe14d..0317e43 100644 --- a/inc/lib.improve.action.zip.php +++ b/inc/lib.improve.action.zip.php @@ -1,16 +1,15 @@ setProperties([ - 'id' => 'zip', - 'name' => __('Zip module'), - 'desc' => __('Compress module into a ready to install package'), + 'id' => 'zip', + 'name' => __('Zip module'), + 'desc' => __('Compress module into a ready to install package'), 'priority' => 980, - 'config' => true, - 'types' => ['plugin', 'theme'] + 'config' => true, + 'types' => ['plugin', 'theme'] ]); return true; @@ -75,7 +74,9 @@ class ImproveActionZip extends ImproveAction

' . - '

' . sprintf(__('Preconization: %s'), $this->core->blog->public_path ? + '

' . sprintf( + __('Preconization: %s'), + $this->core->blog->public_path ? path::real($this->core->blog->public_path) : __("Blog's public directory") ) . '

@@ -93,7 +94,7 @@ class ImproveActionZip extends ImproveAction '

' . sprintf(__('Preconization: %s'), '%type%-%id%-%version%') . '

-

@@ -118,7 +119,7 @@ class ImproveActionZip extends ImproveAction public function closeModule(): ?bool { $exclude = array_merge( - self::$exclude, + self::$exclude, explode(',', $this->getSetting('pack_excludefiles')) ); $this->setSuccess(sprintf(__('Prepare excluded files "%s"'), implode(', ', $exclude))); @@ -132,6 +133,7 @@ class ImproveActionZip extends ImproveAction if (!empty($this->getSetting('secondpack_filename'))) { $this->zipModule($this->getSetting('secondpack_filename'), $exclude); } + return null; } @@ -149,7 +151,7 @@ class ImproveActionZip extends ImproveAction $file ); $parts = explode('/', $file); - foreach($parts as $i => $part) { + foreach ($parts as $i => $part) { $parts[$i] = files::tidyFileName($part); } $path = $this->getSetting('pack_repository') . '/' . implode('/', $parts) . '.zip'; @@ -164,9 +166,9 @@ class ImproveActionZip extends ImproveAction return null; } @set_time_limit(300); - $fp = fopen($path, 'wb'); + $fp = fopen($path, 'wb'); $zip = new ImproveZipFileZip($fp); - foreach($exclude as $e) { + foreach ($exclude as $e) { $e = '#(^|/)(' . str_replace( ['.', '*'], ['\.', '.*?'], @@ -209,9 +211,9 @@ class ImproveZipFileZip extends fileZip } $unc_len = strlen($content); - $crc = crc32($content); - $zdata = gzdeflate($content); - $c_len = strlen($zdata); + $crc = crc32($content); + $zdata = gzdeflate($content); + $c_len = strlen($zdata); unset($content); @@ -219,8 +221,7 @@ class ImproveZipFileZip extends fileZip $mtime = $this->makeTime($mtime); # Data descriptor - $data_desc = - "\x50\x4b\x03\x04" . + $data_desc = "\x50\x4b\x03\x04" . "\x14\x00" . # ver needed to extract "\x00\x00" . # gen purpose bit flag "\x08\x00" . # compression method @@ -243,8 +244,7 @@ class ImproveZipFileZip extends fileZip $new_offset = $this->old_offset + strlen($data_desc); # Add to central directory record - $cdrec = - "\x50\x4b\x01\x02" . + $cdrec = "\x50\x4b\x01\x02" . "\x00\x00" . # version made by "\x14\x00" . # version needed to extract "\x00\x00" . # gen purpose bit flag @@ -260,7 +260,7 @@ class ImproveZipFileZip extends fileZip pack('v', 0) . # disk number start pack('v', 0) . # internal file attributes pack('V', 32) . # external file attributes - 'archive' bit set - pack('V', $this->old_offset). # relative offset of local header + pack('V', $this->old_offset) . # relative offset of local header $name; $this->old_offset = $new_offset; @@ -291,6 +291,7 @@ class ImproveZipFileZip extends fileZip $newStr .= $token; } } + return $newStr; } -} \ No newline at end of file +} diff --git a/index.php b/index.php index 68b07c0..6f5c4c6 100644 --- a/index.php +++ b/index.php @@ -1,16 +1,15 @@ blog->settings->improve->preferences); if (!is_array($preferences)) { @@ -31,7 +30,7 @@ $preferences = array_merge(['plugin' => [], 'theme' => []], $preferences); if (!empty($_POST['save_preferences'])) { $preferences[$type] = []; if (!empty($_POST['actions'])) { - foreach($improve->modules() as $action) { + foreach ($improve->modules() as $action) { if (in_array($type, $action->types) && in_array($action->id, $_POST['actions'])) { $preferences[$type][] = $action->id; } @@ -41,10 +40,10 @@ if (!empty($_POST['save_preferences'])) { dcPage::addSuccessNotice(__('Configuration successfully updated')); } -$allow_distrib = (boolean) $core->blog->settings->improve->allow_distrib; -$official = [ - 'plugin' => explode(',', DC_DISTRIB_PLUGINS), - 'theme' => explode(',', DC_DISTRIB_THEMES) +$allow_distrib = (bool) $core->blog->settings->improve->allow_distrib; +$official = [ + 'plugin' => explode(',', DC_DISTRIB_PLUGINS), + 'theme' => explode(',', DC_DISTRIB_THEMES) ]; if (!isset($core->themes)) { @@ -53,8 +52,8 @@ if (!isset($core->themes)) { } $combo_modules = [__('Select a module') => '-']; -$modules = $type == 'plugin' ? $core->plugins->getModules() : $core->themes->getModules(); -foreach($modules as $id => $m) { +$modules = $type == 'plugin' ? $core->plugins->getModules() : $core->themes->getModules(); +foreach ($modules as $id => $m) { if (!$m['root_writable'] || !$allow_distrib && in_array($id, $official[$type])) { continue; } @@ -73,9 +72,9 @@ if (!empty($_POST['fix'])) { } else { try { $time = $improve->fixModule( - $type, - $module, - $type == 'plugin' ? $core->plugins->getModules($module) : $core->themes->getModules($module), + $type, + $module, + $type == 'plugin' ? $core->plugins->getModules($module) : $core->themes->getModules($module), $_POST['actions'] ); $log_id = $improve->writeLogs(); @@ -102,21 +101,19 @@ if (!empty($_POST['fix'])) { $breadcrumb = []; if (!empty($_REQUEST['config'])) { $breadcrumb = [ - ($type == 'plugin' ? __('Plugins') : __('Themes')) => - $core->adminurl->get('admin.plugin.improve', ['type' => ($type == 'plugin' ? 'plugin' : 'theme')]), - '' . __('Configure module') . '' => '' + ($type == 'plugin' ? __('Plugins') : __('Themes')) => $core->adminurl->get('admin.plugin.improve', ['type' => ($type == 'plugin' ? 'plugin' : 'theme')]), + '' . __('Configure module') . '' => '' ]; } else { $breadcrumb = [ - '' . ($type == 'plugin' ? __('Plugins') : __('Themes')) . '' => '', - ($type == 'theme' ? __('Plugins') : __('Themes')) => - $core->adminurl->get('admin.plugin.improve', ['type' => ($type == 'theme' ? 'plugin' : 'theme')]) + '' . ($type == 'plugin' ? __('Plugins') : __('Themes')) . '' => '', + ($type == 'theme' ? __('Plugins') : __('Themes')) => $core->adminurl->get('admin.plugin.improve', ['type' => ($type == 'theme' ? 'plugin' : 'theme')]) ]; } # display header echo '' . __('improve') . '' . -dcPage::breadcrumb(array_merge([__('improve') => ''], $breadcrumb),['hl' => false]) . +dcPage::breadcrumb(array_merge([__('improve') => ''], $breadcrumb), ['hl' => false]) . dcPage::notices(); if (!empty($_REQUEST['config'])) { @@ -124,14 +121,14 @@ if (!empty($_REQUEST['config'])) { if (null !== ($action = $improve->module($_REQUEST['config']))) { $redir = $_REQUEST['redir'] ?? $core->adminurl->get('admin.plugin.improve', ['type' => $type, 'config' => $action->id]); - $res = $action->configure($redir); + $res = $action->configure($redir); echo '

' . sprintf(__('Configure module "%s"'), $action->name) . '

' . __('Back') . '

' . html::escapeHTML($action->desc) . '

' . - (empty($res) ? '

' . __('Nothing to configure'). '

' : $res) . ' + (empty($res) ? '

' . __('Nothing to configure') . '

' : $res) . '

' . form::hidden('type', $type) . form::hidden('config', $action->id) . @@ -143,37 +140,35 @@ if (!empty($_REQUEST['config'])) {

' . __('Unknow module') . '

' . __('Back') . '

'; } - } else { - echo '

' . ($type == 'plugin' ? __('Plugins') : __('Themes')) . '

'; if (count($combo_modules) == 1) { echo '

' . __('No module to manage') . '

'; } else { echo ''; - foreach($improve->modules() as $action) { + foreach ($improve->modules() as $action) { if (!in_array($type, $action->types)) { continue; } - $p = DC_DEBUG ? '' . $action->priority. ' ' : ''; - echo - '

' . $p . '

' . $p . ''; if (false !== $action->config) { - echo - ' - name) . '">' . __('Configure module') . ''; } echo '

'; @@ -184,11 +179,11 @@ if (!empty($_REQUEST['config'])) {

+ __('Save fields selection as preference') . '

' . form::combo('module', $combo_modules, $module) . '

- ' . + ' . form::hidden(['type'], $type) . $core->formNonce() . '

@@ -203,13 +198,13 @@ if (!empty($_REQUEST['config'])) { if (!empty($logs)) { echo '

' . __('Details') . '

'; - foreach($logs as $path => $types) { - echo '
' . $path .'
'; - foreach($types as $type => $tools) { + foreach ($logs as $path => $types) { + echo '
' . $path . '
'; + foreach ($types as $type => $tools) { echo '
    '; - foreach($tools as $tool => $msgs) { + foreach ($tools as $tool => $msgs) { echo '
  • ' . $improve->module($tool)->name . '
      '; - foreach($msgs as $msg) { + foreach ($msgs as $msg) { echo '
    • ' . $msg . '
    • '; } echo '
  • '; @@ -224,4 +219,4 @@ if (!empty($_REQUEST['config'])) { } } -echo ''; \ No newline at end of file +echo '';