Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP Fixes found by Static Analysis Tool #1261

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
5 changes: 4 additions & 1 deletion phpseclib/Common/Functions/Objects.php
Expand Up @@ -30,6 +30,7 @@ abstract class Objects
* @param string $var
* @return mixed
* @access public
* @throws \ReflectionException
*/
public static function getVar($obj, $var)
{
Expand All @@ -46,6 +47,7 @@ public static function getVar($obj, $var)
* @param string $var
* @param mixed $val
* @access public
* @throws \ReflectionException
*/
public static function setVar($obj, $var, $val)
{
Expand All @@ -63,8 +65,9 @@ public static function setVar($obj, $var, $val)
* @param array $params
* @return mixed
* @access public
* @throws \ReflectionException
*/
public static function callFunc($obj, $func, $params = [])
public static function callFunc($obj, $func, array $params = [])
{
$reflection = new \ReflectionClass(get_class($obj));
$method = $reflection->getMethod($func);
Expand Down
16 changes: 9 additions & 7 deletions phpseclib/Common/Functions/Strings.php
Expand Up @@ -75,12 +75,12 @@ public static function pop(&$string, $index = 1)
*/
public static function equals($x, $y)
{
if (strlen($x) != strlen($y)) {
if (strlen($x) !== strlen($y)) {
return false;
}

$result = 0;
for ($i = 0; $i < strlen($x); $i++) {
for ($i = 0, $iMax = strlen($x); $i < $iMax; $i++) {
$result |= ord($x[$i]) ^ ord($y[$i]);
}

Expand All @@ -107,11 +107,12 @@ public static function equals($x, $y)
* @param $data
* @return mixed
* @access public
* @throws \InvalidArgumentException
*/
public static function unpackSSH2($format, $data)
{
$result = [];
for ($i = 0; $i < strlen($format); $i++) {
for ($i = 0, $iMax = strlen($format); $i < $iMax; $i++) {
switch ($format[$i]) {
case 'C':
case 'b':
Expand All @@ -135,7 +136,7 @@ public static function unpackSSH2($format, $data)
$result[] = ord(self::shift($data));
continue 2;
case 'b':
$result[] = ord(self::shift($data)) != 0;
$result[] = ord(self::shift($data)) !== 0;
continue 2;
case 'N':
list(, $temp) = unpack('N', self::shift($data, 4));
Expand Down Expand Up @@ -165,19 +166,20 @@ public static function unpackSSH2($format, $data)
/**
* Create SSH2-style string
*
* @param $elements[]
* @param $elements []
* @access public
* @return mixed
* @throws \InvalidArgumentException
*/
public static function packSSH2(...$elements)
{
$format = $elements[0];
array_shift($elements);
if (strlen($format) != count($elements)) {
if (strlen($format) !== count($elements)) {
throw new \InvalidArgumentException('There must be as many arguments as there are characters in the $format string');
}
$result = '';
for ($i = 0; $i < strlen($format); $i++) {
for ($i = 0, $iMax = strlen($format); $i < $iMax; $i++) {
$element = $elements[$i];
switch ($format[$i]) {
case 'C':
Expand Down
6 changes: 4 additions & 2 deletions phpseclib/Crypt/Common/AsymmetricKey.php
Expand Up @@ -247,7 +247,7 @@ protected static function initialize_static_variables()
}

self::loadPlugins('Keys');
if (static::ALGORITHM != 'RSA') {
if (static::ALGORITHM !== 'RSA') {
self::loadPlugins('Signature');
}
}
Expand All @@ -263,7 +263,7 @@ private static function loadPlugins($format)
if (!isset(self::$plugins[static::ALGORITHM][$format])) {
self::$plugins[static::ALGORITHM][$format] = [];
foreach (new \DirectoryIterator(__DIR__ . '/../' . static::ALGORITHM . '/' . $format . '/') as $file) {
if ($file->getExtension() != 'php') {
if ($file->getExtension() !== 'php') {
continue;
}
$name = $file->getBasename('.php');
Expand Down Expand Up @@ -403,6 +403,7 @@ public static function getSupportedKeyFormats()
* @param string $fullname
* @access public
* @return bool
* @throws \ReflectionException
*/
public static function addFileFormat($fullname)
{
Expand Down Expand Up @@ -523,6 +524,7 @@ public function setPublicKeyFormat($format)
* @see self::load()
* @access public
* @return mixed
* @throws \ReflectionException
*/
public function getLoadedFormat()
{
Expand Down
10 changes: 5 additions & 5 deletions phpseclib/Crypt/Common/SymmetricKey.php
Expand Up @@ -682,11 +682,11 @@ public function setPassword($password, $method = 'pbkdf2', ...$func_args)
$dkLen = $func_args[3];
} else {
$key_length = $this->explicit_key_length !== false ? $this->explicit_key_length : $this->key_length;
$dkLen = $method == 'pbkdf1' ? 2 * $key_length : $key_length;
$dkLen = $method === 'pbkdf1' ? 2 * $key_length : $key_length;
}

switch (true) {
case $method == 'pkcs12':
case $method === 'pkcs12':
/*
In this specification, however, all passwords are created from
BMPStrings with a NULL terminator. This means that each character in
Expand Down Expand Up @@ -741,7 +741,7 @@ public function setPassword($password, $method = 'pbkdf2', ...$func_args)
}

return true;
case $method == 'pbkdf1':
case $method === 'pbkdf1':
if ($dkLen > $hashObj->getLengthInBytes()) {
throw new \LengthException('Derived key length cannot be longer than the hash length');
}
Expand Down Expand Up @@ -2754,7 +2754,7 @@ protected static function safe_intval($x)
switch (true) {
case is_int($x):
// PHP 5.3, per http://php.net/releases/5_3_0.php, introduced "more consistent float rounding"
case (php_uname('m') & "\xDF\xDF\xDF") != 'ARM':
case (php_uname('m') & "\xDF\xDF\xDF") !== 'ARM':
return $x;
}
return (fmod($x, 0x80000000) & 0x7FFFFFFF) |
Expand All @@ -2771,7 +2771,7 @@ protected static function safe_intval_inline()
{
switch (true) {
case defined('PHP_INT_SIZE') && PHP_INT_SIZE == 8:
case (php_uname('m') & "\xDF\xDF\xDF") != 'ARM':
case (php_uname('m') & "\xDF\xDF\xDF") !== 'ARM':
return '%s';
break;
default:
Expand Down