Increase PHPStan levels and update code to resolve issues found

This commit is contained in:
SignpostMarv
2018-01-27 19:45:35 +00:00
committed by Ben Ramsey
parent a2ba41740c
commit 0030399564
26 changed files with 82 additions and 84 deletions
+2 -2
View File
@@ -68,8 +68,8 @@
"lint": "parallel-lint src tests",
"phpcs": "phpcs src tests --standard=psr2 -sp --colors",
"phpstan": [
"phpstan analyse -c phpstan.neon src --level 2 --no-progress",
"phpstan analyse -c phpstan-tests.neon tests --level 2 --no-progress"
"phpstan analyse -c phpstan.neon src --level max --no-progress",
"phpstan analyse -c phpstan-tests.neon tests --level 5 --no-progress"
],
"phpunit": "phpunit --verbose --colors=always",
"phpunit-coverage": "phpunit --verbose --colors=always --coverage-html build/coverage",
+14
View File
@@ -1,3 +1,17 @@
parameters:
checkMissingIterableValueType: false
autoload_files:
- tests/phpstan-bootstrap.php
ignoreErrors:
-
message: "#^Negated boolean expression is always false\\.$#"
count: 1
path: src/Uuid.php
-
message: '#^Comparison operation "<" between int<6, max> and 0 is always false\.$#'
count: 1
path: src/Generator/CombGenerator.php
-
message: '#^Comparison operation ">" between 6 and 0 is always true\.$#'
count: 1
path: src/Generator/CombGenerator.php
+1 -1
View File
@@ -53,7 +53,7 @@ class GuidStringCodec extends StringCodec
{
$components = array_values($uuid->getFieldsHex());
return hex2bin(implode('', $components));
return (string) hex2bin(implode('', $components));
}
/**
+1 -1
View File
@@ -42,7 +42,7 @@ class OrderedTimeCodec extends StringCodec
$fields['node'],
];
return hex2bin(implode('', $optimized));
return (string) hex2bin(implode('', $optimized));
}
/**
+1 -1
View File
@@ -66,7 +66,7 @@ class StringCodec implements CodecInterface
*/
public function encodeBinary(UuidInterface $uuid)
{
return hex2bin($uuid->getHex());
return (string) hex2bin($uuid->getHex());
}
/**
+1 -1
View File
@@ -52,7 +52,7 @@ class TimestampFirstCombCodec extends StringCodec
{
$stringEncoding = $this->encode($uuid);
return hex2bin(str_replace('-', '', $stringEncoding));
return (string) hex2bin(str_replace('-', '', $stringEncoding));
}
/**
@@ -28,7 +28,7 @@ class DegradedNumberConverter implements NumberConverterInterface
* Throws an `UnsatisfiedDependencyException`
*
* @param string $hex The hexadecimal string representation to convert
* @return void
* @return mixed
* @throws UnsatisfiedDependencyException
*/
public function fromHex($hex)
@@ -44,7 +44,7 @@ class DegradedNumberConverter implements NumberConverterInterface
* Throws an `UnsatisfiedDependencyException`
*
* @param mixed $integer An integer representation to convert
* @return void
* @return string
* @throws UnsatisfiedDependencyException
*/
public function toHex($integer)
+1 -1
View File
@@ -29,7 +29,7 @@ class DegradedTimeConverter implements TimeConverterInterface
*
* @param string $seconds
* @param string $microSeconds
* @return void
* @return string[]
* @throws UnsatisfiedDependencyException if called on a 32-bit system and `Moontoast\Math\BigNumber` is not present
*/
public function calculateTime($seconds, $microSeconds)
+4
View File
@@ -50,6 +50,7 @@ class DegradedUuid extends Uuid
* For degraded UUIDs, throws an `UnsatisfiedDependencyException` when
* called on a 32-bit system
*
* @return array
* @throws UnsatisfiedDependencyException if called on a 32-bit system
*/
public function getFields()
@@ -65,6 +66,7 @@ class DegradedUuid extends Uuid
* For degraded UUIDs, throws an `UnsatisfiedDependencyException` when
* called on a 32-bit system
*
* @return int
* @throws UnsatisfiedDependencyException if called on a 32-bit system
*/
public function getNode()
@@ -81,6 +83,7 @@ class DegradedUuid extends Uuid
* For degraded UUIDs, throws an `UnsatisfiedDependencyException` when
* called on a 32-bit system
*
* @return int
* @throws UnsatisfiedDependencyException if called on a 32-bit system
*/
public function getTimeLow()
@@ -97,6 +100,7 @@ class DegradedUuid extends Uuid
* For degraded UUIDs, throws an `UnsatisfiedDependencyException` when
* called on a 32-bit system
*
* @return int
* @throws UnsatisfiedDependencyException if called on a 32-bit system
* @throws UnsupportedOperationException If this UUID is not a version 1 UUID
*/
+1
View File
@@ -193,6 +193,7 @@ class FeatureSet
* Sets the time provider for use in this environment
*
* @param TimeProviderInterface $timeProvider
* @return void
*/
public function setTimeProvider(TimeProviderInterface $timeProvider)
{
+1 -1
View File
@@ -74,7 +74,7 @@ class CombGenerator implements RandomGeneratorInterface
$lsbTime = str_pad($this->converter->toHex($this->timestamp()), self::TIMESTAMP_BYTES * 2, '0', STR_PAD_LEFT);
return hex2bin(str_pad(bin2hex($hash), $length - self::TIMESTAMP_BYTES, '0') . $lsbTime);
return (string) hex2bin(str_pad(bin2hex($hash), $length - self::TIMESTAMP_BYTES, '0') . $lsbTime);
}
/**
+3 -3
View File
@@ -92,7 +92,7 @@ class DefaultTimeGenerator implements TimeGeneratorInterface
// Create a 60-bit time value as a count of 100-nanosecond intervals
// since 00:00:00.00, 15 October 1582
$timeOfDay = $this->timeProvider->currentTime();
$uuidTime = $this->timeConverter->calculateTime($timeOfDay['sec'], $timeOfDay['usec']);
$uuidTime = $this->timeConverter->calculateTime((string) $timeOfDay['sec'], (string) $timeOfDay['usec']);
$timeHi = BinaryUtils::applyVersion($uuidTime['hi'], 1);
$clockSeqHi = BinaryUtils::applyVariant($clockSeq >> 8);
@@ -109,14 +109,14 @@ class DefaultTimeGenerator implements TimeGeneratorInterface
]
);
return hex2bin($hex);
return (string) hex2bin($hex);
}
/**
* Uses the node provider given when constructing this instance to get
* the node ID (usually a MAC address)
*
* @param string|int $node A node value that may be used to override the node provider
* @param string|int|null $node A node value that may be used to override the node provider
* @return string Hexadecimal representation of the node ID
* @throws InvalidArgumentException
* @throws Exception
+1 -1
View File
@@ -38,6 +38,6 @@ class OpenSslGenerator implements RandomGeneratorInterface
*/
public function generate($length)
{
return openssl_random_pseudo_bytes($length);
return (string) openssl_random_pseudo_bytes($length);
}
}
+4 -4
View File
@@ -40,13 +40,13 @@ class RandomLibAdapter implements RandomGeneratorInterface
*/
public function __construct(Generator $generator = null)
{
$this->generator = $generator;
if ($this->generator === null) {
if ($generator === null) {
$factory = new Factory();
$this->generator = $factory->getHighStrengthGenerator();
$generator = $factory->getHighStrengthGenerator();
}
$this->generator = $generator;
}
/**
+1 -1
View File
@@ -43,7 +43,7 @@ class FallbackNodeProvider implements NodeProviderInterface
* Returns the system node ID by iterating over an array of node providers
* and returning the first non-empty value found
*
* @return string System node ID as a hexadecimal string
* @return string|null System node ID as a hexadecimal string
* @throws Exception
*/
public function getNode()
+16 -9
View File
@@ -38,19 +38,25 @@ class SystemNodeProvider implements NodeProviderInterface
$pattern = '/[^:]([0-9A-Fa-f]{2}([:-])[0-9A-Fa-f]{2}(\2[0-9A-Fa-f]{2}){4})[^:]/';
$matches = [];
// first try a linux specific way
// first try a linux specific way
$node = $this->getSysfs();
// Search the ifconfig output for all MAC addresses and return
// the first one found
if ($node === false) {
if (preg_match_all($pattern, $this->getIfconfig(), $matches, PREG_PATTERN_ORDER)) {
$node = $matches[1][0];
$node = $matches[1][0] ?? false;
}
}
if ($node !== false) {
$node = str_replace([':', '-'], '', $node);
if (is_array($node)) {
$node = $node[0] ?? false;
}
}
return $node;
}
@@ -62,7 +68,7 @@ class SystemNodeProvider implements NodeProviderInterface
*/
protected function getIfconfig()
{
if (strpos(strtolower(ini_get('disable_functions')), 'passthru') !== false) {
if (strpos(strtolower((string) ini_get('disable_functions')), 'passthru') !== false) {
return '';
}
@@ -83,7 +89,7 @@ class SystemNodeProvider implements NodeProviderInterface
break;
}
return ob_get_clean();
return (string) ob_get_clean();
}
/**
@@ -112,12 +118,13 @@ class SystemNodeProvider implements NodeProviderInterface
$macs = array_map('trim', $macs);
// remove invalid entries
$macs = array_filter($macs, function ($mac) {
return
$macs = array_filter($macs, function ($address) {
return (
// localhost adapter
$mac !== '00:00:00:00:00:00' &&
// must match mac adress
preg_match('/^([0-9a-f]{2}:){5}[0-9a-f]{2}$/i', $mac);
$address !== '00:00:00:00:00:00'
// must match mac address
&& preg_match('/^([0-9a-f]{2}:){5}[0-9a-f]{2}$/i', $address)
);
});
$mac = reset($macs);
+2
View File
@@ -49,6 +49,7 @@ class FixedTimeProvider implements TimeProviderInterface
* Sets the `usec` component of the timestamp
*
* @param int $value The `usec` value to set
* @return void
*/
public function setUsec($value)
{
@@ -59,6 +60,7 @@ class FixedTimeProvider implements TimeProviderInterface
* Sets the `sec` component of the timestamp
*
* @param int $value The `sec` value to set
* @return void
*/
public function setSec($value)
{
+10 -8
View File
@@ -233,8 +233,9 @@ class Uuid implements UuidInterface
* Re-constructs the object from its serialized form.
*
* @param string $serialized
* @link http://php.net/manual/en/class.serializable.php
* @return void
* @throws InvalidUuidStringException
* @link http://php.net/manual/en/class.serializable.php
*/
public function unserialize($serialized)
{
@@ -288,7 +289,7 @@ class Uuid implements UuidInterface
*/
public function getClockSeqHiAndReserved()
{
return hexdec($this->getClockSeqHiAndReservedHex());
return (int) hexdec($this->getClockSeqHiAndReservedHex());
}
public function getClockSeqHiAndReservedHex()
@@ -303,7 +304,7 @@ class Uuid implements UuidInterface
*/
public function getClockSeqLow()
{
return hexdec($this->getClockSeqLowHex());
return (int) hexdec($this->getClockSeqLowHex());
}
public function getClockSeqLowHex()
@@ -473,7 +474,7 @@ class Uuid implements UuidInterface
*/
public function getNode()
{
return hexdec($this->getNodeHex());
return (int) hexdec($this->getNodeHex());
}
public function getNodeHex()
@@ -489,7 +490,7 @@ class Uuid implements UuidInterface
*/
public function getTimeHiAndVersion()
{
return hexdec($this->getTimeHiAndVersionHex());
return (int) hexdec($this->getTimeHiAndVersionHex());
}
public function getTimeHiAndVersionHex()
@@ -504,7 +505,7 @@ class Uuid implements UuidInterface
*/
public function getTimeLow()
{
return hexdec($this->getTimeLowHex());
return (int) hexdec($this->getTimeLowHex());
}
public function getTimeLowHex()
@@ -519,7 +520,7 @@ class Uuid implements UuidInterface
*/
public function getTimeMid()
{
return hexdec($this->getTimeMidHex());
return (int) hexdec($this->getTimeMidHex());
}
public function getTimeMidHex()
@@ -549,7 +550,7 @@ class Uuid implements UuidInterface
throw new UnsupportedOperationException('Not a time-based UUID');
}
return hexdec($this->getTimestampHex());
return (int) hexdec($this->getTimestampHex());
}
/**
@@ -625,6 +626,7 @@ class Uuid implements UuidInterface
* Sets the factory used to create UUIDs.
*
* @param UuidFactoryInterface $factory
* @return void
*/
public static function setFactory(UuidFactoryInterface $factory)
{
+7 -2
View File
@@ -85,6 +85,7 @@ class UuidFactory implements UuidFactoryInterface
* Sets the UUID coder-decoder used by this factory
*
* @param CodecInterface $codec
* @return void
*/
public function setCodec(CodecInterface $codec)
{
@@ -125,6 +126,7 @@ class UuidFactory implements UuidFactoryInterface
* Sets the time-based UUID generator this factory will use to generate version 1 UUIDs
*
* @param TimeGeneratorInterface $generator
* @return void
*/
public function setTimeGenerator(TimeGeneratorInterface $generator)
{
@@ -145,6 +147,7 @@ class UuidFactory implements UuidFactoryInterface
* Sets the random UUID generator this factory will use to generate version 4 UUIDs
*
* @param RandomGeneratorInterface $generator
* @return void
*/
public function setRandomGenerator(RandomGeneratorInterface $generator)
{
@@ -155,6 +158,7 @@ class UuidFactory implements UuidFactoryInterface
* Sets the number converter this factory will use
*
* @param NumberConverterInterface $converter
* @return void
*/
public function setNumberConverter(NumberConverterInterface $converter)
{
@@ -175,6 +179,7 @@ class UuidFactory implements UuidFactoryInterface
* Sets the UUID builder this factory will use when creating `Uuid` instances
*
* @param UuidBuilderInterface $builder
* @return void
*/
public function setUuidBuilder(UuidBuilderInterface $builder)
{
@@ -272,7 +277,7 @@ class UuidFactory implements UuidFactoryInterface
* @param string|UuidInterface $ns The UUID namespace to use
* @param string $name The string to hash together with the namespace
* @param int $version The version of UUID to create (3 or 5)
* @param string $hashFunction The hash function to use when hashing together
* @param callable $hashFunction The hash function to use when hashing together
* the namespace and name
* @return UuidInterface
* @throws InvalidUuidStringException
@@ -299,7 +304,7 @@ class UuidFactory implements UuidFactoryInterface
protected function uuidFromHashedName($hash, $version)
{
$timeHi = BinaryUtils::applyVersion(substr($hash, 12, 4), $version);
$clockSeqHi = BinaryUtils::applyVariant(hexdec(substr($hash, 16, 2)));
$clockSeqHi = BinaryUtils::applyVariant((int) hexdec(substr($hash, 16, 2)));
$fields = [
'time_low' => substr($hash, 0, 8),
+1 -3
View File
@@ -40,9 +40,7 @@ class GuidStringCodecTest extends TestCase
protected function tearDown(): void
{
parent::tearDown();
$this->builder = null;
$this->fields = null;
$this->uuid = null;
unset($this->builder, $this->fields, $this->uuid);
}
public function testEncodeUsesFieldsArray()
+1 -3
View File
@@ -47,9 +47,7 @@ class OrderedTimeCodecTest extends TestCase
protected function tearDown(): void
{
parent::tearDown();
$this->builder = null;
$this->uuid = null;
$this->fields = null;
unset($this->builder, $this->uuid, $this->fields);
}
public function testEncodeUsesFieldsArray()
+1 -3
View File
@@ -44,9 +44,7 @@ class StringCodecTest extends TestCase
protected function tearDown(): void
{
parent::tearDown();
$this->builder = null;
$this->uuid = null;
$this->fields = null;
unset($this->builder, $this->uuid, $this->fields);
}
public function testEncodeUsesFieldsArray()
@@ -25,7 +25,7 @@ class PhpTimeConverterTest extends TestCase
];
$converter = new PhpTimeConverter();
$returned = $converter->calculateTime($seconds, $microSeconds);
$returned = $converter->calculateTime((string) $seconds, (string) $microSeconds);
$this->assertEquals($expectedArray, $returned);
}
}
+1 -3
View File
@@ -48,9 +48,7 @@ class DefaultTimeGeneratorTest extends TestCase
protected function tearDown(): void
{
parent::tearDown();
$this->timeProvider = null;
$this->nodeProvider = null;
$this->timeConverter = null;
unset($this->timeProvider, $this->nodeProvider, $this->timeConverter);
Mockery::close();
AspectMock::clean();
}
@@ -26,7 +26,7 @@ class FixedTimeProviderTest extends TestCase
$timestamp = ['sec' => 1458844556, 'usec' => 200997];
$provider = new FixedTimeProvider($timestamp);
$newTimestamp = ['sec' => 1050804050, 'usec' => '30192'];
$newTimestamp = ['sec' => 1050804050, 'usec' => 30192];
$provider->setSec($newTimestamp['sec']);
$provider->setUsec($newTimestamp['usec']);
+3 -32
View File
@@ -650,12 +650,12 @@ class UuidTest extends TestCase
// Check with a recent date
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals('ff6f8cb0-c57d-11e1-9b21-0800200c9a66', $uuid->toString());
$this->assertEquals('ff6f8cb0-c57d-11e1-9b21-0800200c9a66', sprintf('%s', $uuid));
$this->assertEquals('ff6f8cb0-c57d-11e1-9b21-0800200c9a66', sprintf('%s', (string) $uuid));
// Check with an old date
$uuid = Uuid::fromString('0901e600-0154-1000-9b21-0800200c9a66');
$this->assertEquals('0901e600-0154-1000-9b21-0800200c9a66', $uuid->toString());
$this->assertEquals('0901e600-0154-1000-9b21-0800200c9a66', sprintf('%s', $uuid));
$this->assertEquals('0901e600-0154-1000-9b21-0800200c9a66', sprintf('%s', (string) $uuid));
}
/**
@@ -746,7 +746,7 @@ class UuidTest extends TestCase
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid node value');
$uuid = Uuid::uuid1(9223372036854775808);
$uuid = Uuid::uuid1('9223372036854775808');
}
public function testUuid1WithNonHexadecimalNode()
@@ -1034,7 +1034,6 @@ class UuidTest extends TestCase
$this->assertTrue($uuid1->equals($uuid2));
$this->assertFalse($uuid1->equals($uuid3));
$this->assertFalse($uuid1->equals(null));
$this->assertFalse($uuid1->equals(new stdClass()));
}
@@ -1942,20 +1941,6 @@ class UuidTest extends TestCase
$this->assertEquals('4ae71336-e44b-39bf-b9d2-752e234818a5', $uuid->toString());
}
public function testUuid3WithNullName()
{
$uuid = Uuid::uuid3(Uuid::NIL, null);
$this->assertEquals('4ae71336-e44b-39bf-b9d2-752e234818a5', $uuid->toString());
}
public function testUuid3WithFalseName()
{
$uuid = Uuid::uuid3(Uuid::NIL, false);
$this->assertEquals('4ae71336-e44b-39bf-b9d2-752e234818a5', $uuid->toString());
}
public function testUuid3WithZeroName()
{
$uuid = Uuid::uuid3(Uuid::NIL, '0');
@@ -1978,20 +1963,6 @@ class UuidTest extends TestCase
$this->assertEquals('e129f27c-5103-5c5c-844b-cdf0a15e160d', $uuid->toString());
}
public function testUuid5WithNullName()
{
$uuid = Uuid::uuid5(Uuid::NIL, null);
$this->assertEquals('e129f27c-5103-5c5c-844b-cdf0a15e160d', $uuid->toString());
}
public function testUuid5WithFalseName()
{
$uuid = Uuid::uuid5(Uuid::NIL, false);
$this->assertEquals('e129f27c-5103-5c5c-844b-cdf0a15e160d', $uuid->toString());
}
public function testUuid5WithZeroName()
{
$uuid = Uuid::uuid5(Uuid::NIL, '0');