Skip to content

Commit

Permalink
adding grumphp, php-cs-fixer, fixing styles, narrowing namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydiablo committed Apr 26, 2018
1 parent ce94a5f commit d9514f2
Show file tree
Hide file tree
Showing 25 changed files with 271 additions and 205 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor
/composer.lock
/clover.xml
/clover.xml
.php_cs.cache
22 changes: 22 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

$finder = PhpCsFixer\Finder::create()
->exclude('vendor')
->files()
->in(__DIR__)
;

return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules([
'@PSR2' => true,
'@PHPUnit60Migration:risky' => true,
'binary_operator_spaces' => ['align_double_arrow' => true, 'align_equals' => true],
'single_quote' => true,
'array_syntax' => ['syntax' => 'long'],
'concat_space' => ['spacing' => 'one'],
'psr0' => true
])
->setUsingCache(true)
->setFinder($finder);
;
14 changes: 13 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ install:

stages:
- test
- style check
- phpstan analysis
- test with coverage

Expand All @@ -37,15 +38,26 @@ jobs:
allow_failures:
- php: nightly
include:
- stage: style check
php: 7.1
env: TMPDIR=/tmp USE_XDEBUG=false
script:
- composer style-check
- stage: phpstan analysis
php: 7.1
env: TMPDIR=/tmp USE_XDEBUG=false
script:
- composer phpstan
- stage: test with coverage
php: 7.1
env: TMPDIR=/tmp USE_XDEBUG=true
env: TMPDIR=/tmp USE_XDEBUG=true CC_TEST_REPORTER_ID=d71fe75db3fbeba4cbe250e13574e9e1fb5ffbc0f928cb62aae8bea6cd8ecff6
before_script:
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- chmod +x ./cc-test-reporter
- ./cc-test-reporter before-build
script:
- composer test-with-coverage
after_success:
- bash <(curl -s https://codecov.io/bash) -f ./clover.xml
after_script:
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
19 changes: 13 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
},
"autoload": {
"psr-0": {
"Zend_": "src/"
}
"Zend_Crypt_": "src/"
},
"classmap": [
"src/Zend/Crypt.php"
]
},
"autoload-dev": {
"psr-0": {
Expand All @@ -34,15 +37,19 @@
"require-dev": {
"phpunit/phpunit": "^6.0",
"phpstan/phpstan": "^0.9.2",
"jetbrains/phpstorm-stubs": "dev-phpstan"
"jetbrains/phpstorm-stubs": "dev-phpstan",
"friendsofphp/php-cs-fixer": "^2.11",
"maglnet/composer-require-checker": "^0.1.6 | ^0.2.1",
"phpro/grumphp": "^0.14.0"
},
"archive": {
"exclude": ["/tests"]
},
"scripts": {
"test": "phpunit ./tests",
"test-with-coverage": "phpunit --coverage-clover=clover.xml ./tests",
"phpstan": "phpstan analyze -l7 -c phpstan.neon --no-progress ./src --ansi"
"test": "phpunit",
"test-with-coverage": "phpunit --coverage-clover=clover.xml",
"phpstan": "phpstan analyze -l7 -c phpstan.neon --no-progress ./ --ansi",
"style-check": "php-cs-fixer fix --dry-run -vv"
},
"suggest": {
"ext-openssl": "Used in various hashing/crypt algorithms",
Expand Down
17 changes: 17 additions & 0 deletions grumphp.yml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# grumphp.yml
parameters:
tasks:
composer:
strict: true
composer_require_checker:
config_file: require-checker-config.json
phpcsfixer2:
allow_risky: true
config: .php_cs
phpstan:
level: 7
configuration: phpstan.neon
phpunit:
metadata:
priority: 100
always_execute: true
5 changes: 4 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
parameters:
excludes_analyse:
- %rootDir%/../../../tests/*
- %rootDir%/../../../vendor/*
bootstrap: %rootDir%/../../../phpstan-bootstrap.php
ignoreErrors:
- '#Casting to .+ something that.s already .+\.#'
Expand All @@ -15,4 +18,4 @@ parameters:
- '#Variable \$key in isset\(\) always exists and is not nullable\.#'
- '#Variable \$hash in isset\(\) always exists and is not nullable\.#'
# Probably unnecessary is_float check, Zend_Crypt_Math:121
- '#Call to function is_float\(\) will always evaluate to false\.#'
- '#Call to function is_float\(\) will always evaluate to false\.#'
5 changes: 5 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
backupGlobals="true"
stderr="true"
colors="true">
<testsuites>
<testsuite name="default">
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
Expand Down
15 changes: 15 additions & 0 deletions require-checker-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"symbol-whitelist" : [
"null", "true", "false",
"static", "self", "parent",
"array", "string", "int", "float", "bool", "iterable", "callable", "void", "object",
"bcadd", "bccomp", "bcdiv", "bcmod", "bcmul", "bcpow", "bcpowmod", "bcsqrt", "bcsub",
"gmp_add", "gmp_cmp", "gmp_div", "gmp_mod", "gmp_mul", "gmp_pow", "gmp_powm", "gmp_sqrt", "gmp_strval", "gmp_sub",
"hash", "hash_algos", "hash_hmac",
"mcrypt_create_iv", "MCRYPT_DEV_URANDOM",
"mhash", "mhash_count", "mhash_get_hash_name",
"OPENSSL_ALGO_DSS1", "OPENSSL_ALGO_MD2", "OPENSSL_ALGO_MD4", "OPENSSL_ALGO_MD5", "OPENSSL_ALGO_SHA1",
"openssl_dh_compute_key", "openssl_digest", "openssl_get_privatekey", "openssl_get_publickey", "openssl_pkey_export",
"openssl_pkey_get_details", "openssl_pkey_new", "openssl_sign", "openssl_verify"
]
}
16 changes: 7 additions & 9 deletions src/Zend/Crypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
*/
class Zend_Crypt
{

const TYPE_OPENSSL = 'openssl';
const TYPE_HASH = 'hash';
const TYPE_MHASH = 'mhash';
const TYPE_HASH = 'hash';
const TYPE_MHASH = 'mhash';

protected static $_type = null;

Expand Down Expand Up @@ -86,7 +85,7 @@ public static function hash($algorithm, $data, $binaryOutput = false)
}
self::_detectHashSupport($algorithm);
$supportedMethod = '_digest' . ucfirst(self::$_type);
$result = self::$supportedMethod($algorithm, $data, $binaryOutput);
$result = self::$supportedMethod($algorithm, $data, $binaryOutput);
return $result;
}

Expand All @@ -99,13 +98,13 @@ protected static function _detectHashSupport($algorithm)
if (function_exists('hash')) {
self::$_type = self::TYPE_HASH;
if (in_array($algorithm, hash_algos())) {
return;
return;
}
}
if (function_exists('mhash')) {
self::$_type = self::TYPE_MHASH;
if (in_array($algorithm, self::$_supportedAlgosMhash)) {
return;
return;
}
}
if (function_exists('openssl_digest')) {
Expand All @@ -114,7 +113,7 @@ protected static function _detectHashSupport($algorithm)
}
self::$_type = self::TYPE_OPENSSL;
if (in_array($algorithm, self::$_supportedAlgosOpenssl)) {
return;
return;
}
}
throw new Zend_Crypt_Exception('\'' . $algorithm . '\' is not supported by any available extension or native function');
Expand All @@ -140,7 +139,7 @@ protected static function _digestHash($algorithm, $data, $binaryOutput)
protected static function _digestMhash($algorithm, $data, $binaryOutput)
{
$constant = constant('MHASH_' . strtoupper($algorithm));
$binary = mhash($constant, $data);
$binary = mhash($constant, $data);
if ($binaryOutput) {
return $binary;
}
Expand All @@ -160,5 +159,4 @@ protected static function _digestOpenssl($algorithm, $data, $binaryOutput)
}
return openssl_digest($data, $algorithm, $binaryOutput);
}

}
7 changes: 3 additions & 4 deletions src/Zend/Crypt/DiffieHellman.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ public function __construct($prime, $generator, $privateKey = null, $privateKeyT
public function generateKeys()
{
if (function_exists('openssl_dh_compute_key') && self::$useOpenssl !== false) {
$details = array();
$details = array();
$details['p'] = $this->getPrime();
$details['g'] = $this->getGenerator();
if ($this->hasPrivateKey()) {
$details['priv_key'] = $this->getPrivateKey();
}
$opensslKeyResource = openssl_pkey_new( array('dh' => $details) );
$data = openssl_pkey_get_details($opensslKeyResource);
$opensslKeyResource = openssl_pkey_new(array('dh' => $details));
$data = openssl_pkey_get_details($opensslKeyResource);
$this->setPrivateKey($data['dh']['priv_key'], self::BINARY);
$this->setPublicKey($data['dh']['pub_key'], self::BINARY);
} else {
Expand Down Expand Up @@ -371,5 +371,4 @@ protected function _generatePrivateKey()
$rand = $this->_math->rand($this->getGenerator(), $this->getPrime());
return $rand;
}

}
6 changes: 2 additions & 4 deletions src/Zend/Crypt/Hmac.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected static function _setHashAlgorithm($hash)
throw new Zend_Crypt_Hmac_Exception('provided hash string is null or empty');
}

$hash = strtolower($hash);
$hash = strtolower($hash);
$hashSupported = false;

if (function_exists('hash_algos') && in_array($hash, hash_algos())) {
Expand Down Expand Up @@ -166,11 +166,9 @@ protected static function _getMhashDefinition($hashAlgorithm)
{
$types = array();

for ($i = 0; $i <= mhash_count(); $i++)
{
for ($i = 0; $i <= mhash_count(); $i++) {
$types[mhash_get_hash_name($i)] = $i;
}
return $types[strtoupper($hashAlgorithm)];
}

}
6 changes: 3 additions & 3 deletions src/Zend/Crypt/Math.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function rand($minimum, $maximum)
return mt_rand($minimum, $maximum - 1);
}
$rand = '';
$i2 = strlen($maximum) - 1;
$i2 = strlen($maximum) - 1;
for ($i = 1; $i < $i2; $i++) {
$rand .= mt_rand(0, 9);
}
Expand Down Expand Up @@ -127,7 +127,7 @@ public static function randInteger($min, $max, $strong = false)
return random_int($min, $max);
}
// calculate number of bits required to store range on this machine
$r = $range;
$r = $range;
$bits = 0;
while ($r) {
$bits++;
Expand All @@ -137,7 +137,7 @@ public static function randInteger($min, $max, $strong = false)
$bytes = (int) max(ceil($bits / 8), 1);
$filter = (int) ((1 << $bits) - 1);
do {
$rnd = hexdec(bin2hex(self::randBytes($bytes, $strong)));
$rnd = hexdec(bin2hex(self::randBytes($bytes, $strong)));
$rnd &= $filter;
} while ($rnd > $range);
return ($min + $rnd);
Expand Down
5 changes: 2 additions & 3 deletions src/Zend/Crypt/Math/BigInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function __construct($extension = null)
*/
public function __call($methodName, $args)
{
if(!method_exists($this->_math, $methodName)) {
if (!method_exists($this->_math, $methodName)) {
throw new Zend_Crypt_Math_BigInteger_Exception('invalid method call: ' . get_class($this->_math) . '::' . $methodName . '() does not exist');
}
return call_user_func_array(array($this->_math, $methodName), $args);
Expand All @@ -113,7 +113,7 @@ protected function _loadAdapter($extension = null)
$extension = 'bcmath';
}
}
if($extension == 'gmp' && extension_loaded('gmp')) {
if ($extension == 'gmp' && extension_loaded('gmp')) {
$this->_math = new Zend_Crypt_Math_BigInteger_Gmp();
//} elseif($extension == 'bigint' && extension_loaded('big_int')) {
// $this->_math = new Zend_Crypt_Math_BigInteger_Bigint();
Expand All @@ -123,5 +123,4 @@ protected function _loadAdapter($extension = null)
throw new Zend_Crypt_Math_BigInteger_Exception($extension . ' big integer precision math support not detected');
}
}

}
14 changes: 7 additions & 7 deletions src/Zend/Crypt/Math/BigInteger/Bcmath.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ public function binaryToInteger($operand)
{
$result = '0';
while (strlen($operand)) {
$ord = ord(substr($operand, 0, 1));
$result = bcadd(bcmul($result, 256), $ord);
$ord = ord(substr($operand, 0, 1));
$result = bcadd(bcmul($result, 256), $ord);
$operand = substr($operand, 1);
}
return $result;
Expand All @@ -180,13 +180,13 @@ public function binaryToInteger($operand)
*/
public function integerToBinary($operand)
{
$cmp = bccomp($operand, 0);
$cmp = bccomp($operand, 0);
$return = '';
if ($cmp == 0) {
return "\0";
}
while (bccomp($operand, 0) > 0) {
$return = chr(bcmod($operand, 256)) . $return;
$return = chr(bcmod($operand, 256)) . $return;
$operand = bcdiv($operand, 256);
}
if (ord($return[0]) > 127) {
Expand All @@ -212,9 +212,9 @@ public function integerToBinary($operand)
public function hexToDecimal($operand)
{
$return = '0';
while(strlen($hex)) {
$hex = hexdec(substr($operand, 0, 4));
$dec = bcadd(bcmul($return, 65536), $hex);
while (strlen($hex)) {
$hex = hexdec(substr($operand, 0, 4));
$dec = bcadd(bcmul($return, 65536), $hex);
$operand = substr($operand, 4);
}
return $return;
Expand Down
Loading

0 comments on commit d9514f2

Please sign in to comment.