Skip to content

Commit

Permalink
QA: remove some unnecessary micro-optimizations
Browse files Browse the repository at this point in the history
While it is not a bad thing to optimize for performance, some micro-optimizations, like saving information to a function local `static` variable, should only be done when the trade-off is worth it.

Having the function local `static` variable will often prevent code coverage from being recorded correctly for the code as the code for setting the static will only be run when the function is first called, which may not be in the dedicated test for the function, so it makes code coverage recording highly dependant on the order in which tests are run.

In these three cases, I do not think this trade-off is worth it as these are a) not the most popular functions, b) true micro-optimizations which only effectively save one fast PHP native `version_compare()` call.
  • Loading branch information
jrfnl committed May 4, 2022
1 parent 41fe01d commit 858f702
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 20 deletions.
12 changes: 4 additions & 8 deletions PHPCSUtils/BackCompat/Helper.php
Expand Up @@ -182,14 +182,10 @@ public static function getTabWidth(File $phpcsFile)
*/
public static function getEncoding(File $phpcsFile = null)
{
static $default;

if (isset($default) === false) {
$default = 'utf-8';
if (\version_compare(self::getVersion(), '2.99.99', '<=') === true) {
// In PHPCS 2.x, the default encoding is `iso-8859-1`.
$default = 'iso-8859-1';
}
$default = 'utf-8';
if (\version_compare(self::getVersion(), '2.99.99', '<=') === true) {
// In PHPCS 2.x, the default encoding is `iso-8859-1`.
$default = 'iso-8859-1';
}

if ($phpcsFile instanceof File) {
Expand Down
7 changes: 1 addition & 6 deletions PHPCSUtils/Utils/MessageHelper.php
Expand Up @@ -120,12 +120,7 @@ public static function stringToErrorcode($text)
*/
public static function hasNewLineSupport()
{
static $supported;
if (isset($supported) === false) {
$supported = \version_compare(Helper::getVersion(), '3.3.1', '>=');
}

return $supported;
return \version_compare(Helper::getVersion(), '3.3.1', '>=');
}

/**
Expand Down
8 changes: 2 additions & 6 deletions PHPCSUtils/Utils/Orthography.php
Expand Up @@ -102,13 +102,9 @@ public static function isFirstCharLowercase($textString)
*/
public static function isLastCharPunctuation($textString, $allowedChars = self::TERMINAL_POINTS)
{
static $encoding;

if (isset($encoding) === false) {
$encoding = Helper::getEncoding();
}

$encoding = Helper::getEncoding();
$textString = \rtrim($textString);

if (\function_exists('iconv_substr') === true) {
$lastChar = \iconv_substr($textString, -1, 1, $encoding);
} else {
Expand Down

0 comments on commit 858f702

Please sign in to comment.