Skip to content

Commit

Permalink
Code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
alecpl committed Mar 23, 2024
1 parent b1596b7 commit 49a40ea
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 196 deletions.
150 changes: 0 additions & 150 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,145 +1,5 @@
parameters:
ignoreErrors:
-
message: "#^Method example_addressbook_backend\\:\\:add_to_group\\(\\) should return int but returns false\\.$#"
count: 1
path: plugins/example_addressbook/example_addressbook_backend.php

-
message: "#^Method example_addressbook_backend\\:\\:remove_from_group\\(\\) should return int but returns false\\.$#"
count: 1
path: plugins/example_addressbook/example_addressbook_backend.php

-
message: "#^Method Identicon_Plugin\\:\\:setUpBeforeCLass\\(\\) does not match parent method name\\: PHPUnit\\\\Framework\\\\TestCase\\:\\:setUpBeforeClass\\(\\)\\.$#"
count: 1
path: plugins/identicon/tests/Identicon.php

-
message: "#^Foreach overwrites \\$skin with its value variable\\.$#"
count: 1
path: plugins/jqueryui/jqueryui.php

-
message: "#^Method rcube_sieve\\:\\:__construct\\(\\) with return type void returns mixed but should not return anything\\.$#"
count: 2
path: plugins/managesieve/lib/Roundcube/rcube_sieve.php

-
message: "#^Access to an undefined property rcube_sieve_engine\\:\\:\\$master_file\\.$#"
count: 1
path: plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php

-
message: "#^Call to function is_array\\(\\) with array will always evaluate to true\\.$#"
count: 2
path: plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php

-
message: "#^Comparison operation \"\\>\" between int\\<1, max\\> and 0 is always true\\.$#"
count: 1
path: plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php

-
message: "#^Foreach overwrites \\$rid with its key variable\\.$#"
count: 1
path: plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php

-
message: "#^Result of && is always false\\.$#"
count: 1
path: plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php

-
message: "#^Strict comparison using \\=\\=\\= between string and null will always evaluate to false\\.$#"
count: 1
path: plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php

-
message: "#^Call to function is_array\\(\\) with array will always evaluate to true\\.$#"
count: 1
path: plugins/managesieve/lib/Roundcube/rcube_sieve_script.php

-
message: "#^Call to function is_array\\(\\) with string will always evaluate to false\\.$#"
count: 2
path: plugins/managesieve/lib/Roundcube/rcube_sieve_script.php

-
message: "#^Implicit array creation is not allowed \\- variable \\$tests might not exist\\.$#"
count: 9
path: plugins/managesieve/lib/Roundcube/rcube_sieve_script.php

-
message: "#^Loose comparison using \\=\\= between 'INGO' and 'INGO' will always evaluate to true\\.$#"
count: 1
path: plugins/managesieve/lib/Roundcube/rcube_sieve_script.php

-
message: "#^Method rcube_sieve_script\\:\\:_tokenize_rule\\(\\) should return array but returns null\\.$#"
count: 2
path: plugins/managesieve/lib/Roundcube/rcube_sieve_script.php

-
message: "#^Method rcube_sieve_script\\:\\:update_rule\\(\\) should return bool but returns int\\.$#"
count: 1
path: plugins/managesieve/lib/Roundcube/rcube_sieve_script.php

-
message: "#^Result of && is always false\\.$#"
count: 1
path: plugins/managesieve/lib/Roundcube/rcube_sieve_script.php

-
message: "#^Unreachable statement \\- code above always terminates\\.$#"
count: 1
path: plugins/managesieve/lib/Roundcube/rcube_sieve_script.php

-
message: "#^Variable \\$i might not be defined\\.$#"
count: 1
path: plugins/managesieve/lib/Roundcube/rcube_sieve_script.php

-
message: "#^Variable \\$pos might not be defined\\.$#"
count: 3
path: plugins/managesieve/lib/Roundcube/rcube_sieve_script.php

-
message: "#^Call to function is_array\\(\\) with array will always evaluate to true\\.$#"
count: 1
path: plugins/managesieve/lib/Roundcube/rcube_sieve_vacation.php

-
message: "#^Implicit array creation is not allowed \\- variable \\$date_value does not exist\\.$#"
count: 1
path: plugins/managesieve/lib/Roundcube/rcube_sieve_vacation.php

-
message: "#^Implicit array creation is not allowed \\- variable \\$vacation does not exist\\.$#"
count: 1
path: plugins/managesieve/lib/Roundcube/rcube_sieve_vacation.php

-
message: "#^Implicit array creation is not allowed \\- variable \\$vacation_action does not exist\\.$#"
count: 1
path: plugins/managesieve/lib/Roundcube/rcube_sieve_vacation.php

-
message: "#^Variable variables are not allowed\\.$#"
count: 3
path: plugins/managesieve/lib/Roundcube/rcube_sieve_vacation.php

-
message: "#^Access to an undefined property rcube\\:\\:\\$action\\.$#"
count: 1
path: plugins/managesieve/managesieve.php

-
message: "#^Access to an undefined property rcube\\:\\:\\$task\\.$#"
count: 1
path: plugins/managesieve/managesieve.php

-
message: "#^Method HTTPSocket\\:\\:fetch_header\\(\\) should return array but returns string\\.$#"
count: 1
Expand Down Expand Up @@ -240,16 +100,6 @@ parameters:
count: 1
path: plugins/password/password.php

-
message: "#^Implicit array creation is not allowed \\- variable \\$rec might not exist\\.$#"
count: 1
path: plugins/squirrelmail_usercopy/squirrelmail_usercopy.php

-
message: "#^Right side of && is always true\\.$#"
count: 1
path: plugins/vcard_attachments/vcard_attachments.php

-
message: "#^Foreach overwrites \\$cid with its value variable\\.$#"
count: 1
Expand Down
4 changes: 2 additions & 2 deletions plugins/example_addressbook/example_addressbook_backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@ public function rename_group($gid, $newname, &$newid)

public function add_to_group($group_id, $ids)
{
return false;
return 0;
}

public function remove_from_group($group_id, $ids)
{
return false;
return 0;
}
}
2 changes: 1 addition & 1 deletion plugins/identicon/tests/Identicon.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Identicon_Plugin extends TestCase
{
public static function setUpBeforeCLass(): void
public static function setUpBeforeClass(): void
{
include_once __DIR__ . '/../identicon.php';
}
Expand Down
1 change: 0 additions & 1 deletion plugins/jqueryui/jqueryui.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public function init()
$this->include_script('js/jquery-ui.min.js');

// include UI stylesheet
$skin = $rcmail->config->get('skin');
$ui_map = $rcmail->config->get('jquery_ui_skin_map', self::$skin_map);
$skins = array_keys($rcmail->output->skins);
$skins[] = 'elastic';
Expand Down
10 changes: 8 additions & 2 deletions plugins/managesieve/lib/Roundcube/rcube_sieve.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public function __construct($username, $password = '', $host = 'localhost', $por
$result = $this->sieve->connect($host, $port, $options, $usetls);

if (is_a($result, 'PEAR_Error')) {
return $this->_set_error(self::ERROR_CONNECTION);
$this->_set_error(self::ERROR_CONNECTION);
return;
}

$authz = null;
Expand All @@ -98,7 +99,8 @@ public function __construct($username, $password = '', $host = 'localhost', $por
$result = $this->sieve->login($username, $password, $auth_type ? strtoupper($auth_type) : null, $authz);

if (is_a($result, 'PEAR_Error')) {
return $this->_set_error(self::ERROR_LOGIN);
$this->_set_error(self::ERROR_LOGIN);
return;
}

$this->exts = $this->get_extensions();
Expand Down Expand Up @@ -291,6 +293,8 @@ public function remove($name = null)

/**
* Gets list of supported by server Sieve extensions
*
* @return array|false
*/
public function get_extensions()
{
Expand Down Expand Up @@ -321,6 +325,8 @@ public function get_extensions()

/**
* Gets list of scripts from server
*
* @return array|false
*/
public function get_scripts()
{
Expand Down
14 changes: 8 additions & 6 deletions plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class rcube_sieve_engine
protected $errors;
protected $form;
protected $list;
protected $master_file;
protected $tips = [];
protected $script = [];
protected $exts = [];
Expand Down Expand Up @@ -1475,7 +1476,7 @@ public function filter_form($attrib)
$_SESSION['managesieve-compact-form'] = $compact;

// do not allow creation of new filters
if ($fid === null && in_array('new_filter', $this->disabled_actions)) {
if ($fid === '' && in_array('new_filter', $this->disabled_actions)) {
$this->rc->output->show_message('managesieve.disabledaction', 'error');
return;
}
Expand Down Expand Up @@ -2248,7 +2249,7 @@ public function action_div($fid, $id, $div = true)

if ($action['type'] == 'redirect') {
$parts = explode('@', $action['target']);
if (count($parts) > 0) {
if (count($parts) > 1) {
$action['domain'] = array_pop($parts);
$action['target'] = implode('@', $parts);
}
Expand Down Expand Up @@ -2823,7 +2824,7 @@ protected function mod_mailbox($mailbox, $mode = 'out')
/**
* List sieve scripts
*
* @return array Scripts list
* @return array|false Scripts list
*/
public function list_scripts()
{
Expand Down Expand Up @@ -3046,14 +3047,15 @@ public function deactivate_script($name)
$name .= $extension;
$rid = 0;

foreach ($script as $rid => $rules) {
foreach ($script as $rules) {
foreach ($rules['actions'] as $action) {
if ($action['type'] == 'include' && empty($action['global'])
&& $action['target'] == $name
) {
break 2;
}
}
$rid++;
}

// Entry found
Expand Down Expand Up @@ -3396,7 +3398,7 @@ protected function action_email_input($i, $field)
// According to RFC5230 the :from string must specify a valid [RFC2822] mailbox-list
// we'll try to extract addresses and validate them separately
$from = rcube_mime::decode_address_list($this->form['actions'][$i][$field], null, true, RCUBE_CHARSET);
foreach ((array) $from as $idx => $addr) {
foreach ($from as $idx => $addr) {
if (empty($addr['mailto']) || !rcube_utils::check_email($addr['mailto'])) {
$this->errors['actions'][$i][$field] = $this->plugin->gettext('noemailwarning');
break;
Expand All @@ -3406,7 +3408,7 @@ protected function action_email_input($i, $field)
}

// Only one address is allowed (at least on cyrus imap)
if (is_array($from) && count($from) > 1) {
if (count($from) > 1) {
$this->errors['actions'][$i][$field] = $this->plugin->gettext('noemailwarning');
}

Expand Down
17 changes: 10 additions & 7 deletions plugins/managesieve/lib/Roundcube/rcube_sieve_script.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function size()
* @param int $index Rule index
* @param array $content Rule content (as array)
*
* @return bool True on success, False otherwise
* @return int|false Rule index on success, False otherwise
*/
public function update_rule($index, $content)
{
Expand All @@ -146,7 +146,8 @@ public function update_rule($index, $content)
public function set_var($name, $value, $mods = [])
{
// Check if variable exists
for ($i = 0, $len = count($this->vars); $i < $len; $i++) {
$i = 0;
for ($len = count($this->vars); $i < $len; $i++) {
if ($this->vars[$i]['name'] == $name) {
break;
}
Expand Down Expand Up @@ -653,6 +654,7 @@ private function _parse_text($script)
$this->set_var($matches[1], $matches[2]);
}
// Horde-Ingo format
// @phpstan-ignore-next-line
elseif (!empty($options['format']) && $options['format'] == 'INGO'
&& preg_match('/^# (.*)/', $line, $matches)
) {
Expand Down Expand Up @@ -711,7 +713,7 @@ private function _parse_text($script)
* @param string $content The whole script content
* @param int &$position Start position in the script
*
* @return array Rule data
* @return array|null Rule data
*/
private function _tokenize_rule($content, &$position)
{
Expand All @@ -724,6 +726,7 @@ private function _tokenize_rule($content, &$position)
$join = false;
$join_not = false;
$length = strlen($content);
$tests = [];

// disabled rule (false + comment): if false # .....
if (preg_match('/^\s*false\s+#\s*/i', substr($content, $position, 20), $m)) {
Expand Down Expand Up @@ -906,7 +909,7 @@ private function _tokenize_rule($content, &$position)
* @param int &$position Start position in the script
* @param string $end End of text separator
*
* @return array Array of parsed action type/target pairs
* @return array|null Array of parsed action type/target pairs
*/
private function _parse_actions($content, &$position, $end = '}')
{
Expand Down Expand Up @@ -1185,7 +1188,7 @@ private function action_arguments(&$tokens, $bool_args, $val_args = [])
* Escape special chars into quoted string value or multi-line string
* or list of strings
*
* @param string $str Text or array (list) of strings
* @param array|string $str Text or array (list) of strings
*
* @return string Result text
*/
Expand Down Expand Up @@ -1255,7 +1258,8 @@ public static function tokenize($str, $num = 0, &$position = 0)
switch ($str[$position]) {
// Quoted string
case '"':
for ($pos = $position + 1; $pos < $length; $pos++) {
$pos = $position + 1;
for ($pos; $pos < $length; $pos++) {
if ($str[$pos] == '"') {
break;
}
Expand All @@ -1281,7 +1285,6 @@ public static function tokenize($str, $num = 0, &$position = 0)
case ']':
$position++;
return $result;
break;
// list/test separator (<< reindent once https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/7179 is fixed)
case ',':
// command separator (<< reindent once https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/7179 is fixed)
Expand Down
Loading

0 comments on commit 49a40ea

Please sign in to comment.