Require phpstan and correct issues found up to level 2

* issues found by phpstan L0
* issues found by phpstan L1
* issues found by phpstan L2 in src/
* issues found by phpstan L2 in tests/
This commit is contained in:
Martin Hujer
2017-11-10 10:36:50 +01:00
committed by Ben Ramsey
parent 1eccd070c2
commit ca2629767d
23 changed files with 106 additions and 37 deletions
+1
View File
@@ -53,6 +53,7 @@ before_script:
script:
- ./resources/scripts/cmd-proxy.sh ./vendor/bin/parallel-lint src tests
- ./resources/scripts/cmd-proxy.sh ./vendor/bin/phpcs src tests --standard=psr2 -sp --colors
- ./resources/scripts/cmd-proxy.sh composer run phpstan
- travis_wait ./resources/scripts/cmd-proxy.sh ./vendor/bin/phpunit --verbose --coverage-clover build/logs/clover.xml
after_success:
+9
View File
@@ -23,6 +23,10 @@
"moontoast/math": "^1.1",
"paragonie/random-lib": "^2",
"php-mock/php-mock-phpunit": "^2.5",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "^0.12",
"phpstan/phpstan-mockery": "^0.12",
"phpstan/phpstan-phpunit": "^0.12",
"phpunit/phpunit": "^8.5",
"squizlabs/php_codesniffer": "^3.5"
},
@@ -63,11 +67,16 @@
"scripts": {
"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"
],
"phpunit": "phpunit --verbose --colors=always",
"phpunit-coverage": "phpunit --verbose --colors=always --coverage-html build/coverage",
"test": [
"@lint",
"@phpcs",
"@phpstan",
"@phpunit"
]
},
+7
View File
@@ -0,0 +1,7 @@
parameters:
ignoreErrors:
- '#Function uuid_create not found#'
- '#Function uuid_parse not found#'
- '#Constant UUID_TYPE_TIME not found#'
- '#Constant UUID_TYPE_RANDOM not found#'
- '#Cannot cast Ramsey\\Uuid\\UuidInterface to string.#'
+6
View File
@@ -0,0 +1,6 @@
parameters:
ignoreErrors:
- '#Function uuid_create not found#'
- '#Function uuid_parse not found#'
- '#Constant UUID_TYPE_TIME not found#'
- '#Constant UUID_TYPE_RANDOM not found#'
+1 -1
View File
@@ -10,7 +10,7 @@ class BinaryUtils
/**
* Applies the RFC 4122 variant field to the `clock_seq_hi_and_reserved` field
*
* @param $clockSeqHi
* @param int $clockSeqHi
* @return int The high field of the clock sequence multiplexed with the variant
* @link http://tools.ietf.org/html/rfc4122#section-4.1.1
*/
+1 -1
View File
@@ -36,7 +36,7 @@ class PhpTimeConverter implements TimeConverterInterface
{
// 0x01b21dd213814000 is the number of 100-ns intervals between the
// UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
$uuidTime = ($seconds * 10000000) + ($microSeconds * 10) + 0x01b21dd213814000;
$uuidTime = ((int) $seconds * 10000000) + ((int) $microSeconds * 10) + 0x01b21dd213814000;
return [
'low' => sprintf('%08x', $uuidTime & 0xffffffff),
+1 -1
View File
@@ -283,7 +283,7 @@ class FeatureSet
* Determines which time converter to use and returns the configured
* time converter for this environment
*
* @return TimeConverterInterface
* @return \Ramsey\Uuid\Converter\TimeConverterInterface
*/
protected function buildTimeConverter()
{
+1 -1
View File
@@ -33,7 +33,7 @@ class FixedTimeProvider implements TimeProviderInterface
/**
* Constructs a `FixedTimeProvider` using the provided `$timestamp`
*
* @param int[] Array containing `sec` and `usec` components of a timestamp
* @param int[] $timestamp Array containing `sec` and `usec` components of a timestamp
* @throws InvalidArgumentException if the `$timestamp` does not contain `sec` or `usec` components
*/
public function __construct(array $timestamp)
+1
View File
@@ -238,6 +238,7 @@ class Uuid implements UuidInterface
*/
public function unserialize($serialized)
{
/** @var \Ramsey\Uuid\Uuid $uuid */
$uuid = self::fromString($serialized);
$this->codec = $uuid->codec;
$this->converter = $uuid->converter;
+5 -3
View File
@@ -2,6 +2,7 @@
namespace Ramsey\Uuid\Test\Codec;
use PHPUnit\Framework\MockObject\MockObject;
use Ramsey\Uuid\Builder\UuidBuilderInterface;
use Ramsey\Uuid\Codec\GuidStringCodec;
use Ramsey\Uuid\Test\TestCase;
@@ -14,11 +15,12 @@ use Ramsey\Uuid\UuidInterface;
*/
class GuidStringCodecTest extends TestCase
{
/** @var UuidBuilderInterface */
/** @var UuidBuilderInterface&MockObject */
private $builder;
/** @var UuidInterface */
/** @var UuidInterface&MockObject */
private $uuid;
/** @var array */
private $fields;
+7 -2
View File
@@ -2,6 +2,7 @@
namespace Ramsey\Uuid\Test\Codec;
use PHPUnit\Framework\MockObject\MockObject;
use Ramsey\Uuid\Builder\UuidBuilderInterface;
use Ramsey\Uuid\Codec\OrderedTimeCodec;
use Ramsey\Uuid\Test\TestCase;
@@ -15,14 +16,18 @@ use Ramsey\Uuid\UuidInterface;
class OrderedTimeCodecTest extends TestCase
{
/** @var UuidBuilderInterface */
/** @var UuidBuilderInterface&MockObject */
private $builder;
/** @var UuidInterface */
/** @var UuidInterface&MockObject */
private $uuid;
/** @var array */
private $fields;
/** @var string */
private $uuidString = '58e0a7d7-eebc-11d8-9669-0800200c9a66';
/** @var string */
private $optimizedHex = '11d8eebc58e0a7d796690800200c9a66';
+6 -2
View File
@@ -2,6 +2,7 @@
namespace Ramsey\Uuid\Test\Codec;
use PHPUnit\Framework\MockObject\MockObject;
use Ramsey\Uuid\Builder\UuidBuilderInterface;
use Ramsey\Uuid\Codec\StringCodec;
use Ramsey\Uuid\Test\TestCase;
@@ -15,12 +16,15 @@ use Ramsey\Uuid\UuidInterface;
class StringCodecTest extends TestCase
{
/** @var UuidBuilderInterface */
/** @var UuidBuilderInterface&MockObject */
private $builder;
/** @var UuidInterface */
/** @var UuidInterface&MockObject */
private $uuid;
/** @var array */
private $fields;
/** @var string */
private $uuidString = '12345678-1234-abcd-abef-1234abcd4321';
@@ -1,6 +1,6 @@
<?php
namespace Ramsey\Uuid\Test\Converter;
namespace Ramsey\Uuid\Test\Converter\Time;
use Ramsey\Uuid\Converter\Time\PhpTimeConverter;
use Ramsey\Uuid\Test\TestCase;
@@ -8,7 +8,7 @@ use Ramsey\Uuid\Test\TestCase;
/**
* Class PhpTimeConverterTest
* @package Ramsey\Uuid\Test\Converter
* @covers Ramsey\Uuid\Converter\Time\PhpTimeConverter
* @covers \Ramsey\Uuid\Converter\Time\PhpTimeConverter
*/
class PhpTimeConverterTest extends TestCase
{
@@ -1,7 +1,7 @@
<?php
namespace Ramsey\Uuid\Test\Encoder;
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit\Framework\MockObject\MockObject;
use Ramsey\Uuid\Builder\UuidBuilderInterface;
use Ramsey\Uuid\Codec\CodecInterface;
use Ramsey\Uuid\Codec\TimestampFirstCombCodec;
@@ -16,7 +16,7 @@ class TimestampFirstCombCodecTest extends TestCase
private $codec;
/**
* @var PHPUnit_Framework_MockObject_MockObject
* @var MockObject
*/
private $builderMock;
+2 -2
View File
@@ -1,7 +1,7 @@
<?php
namespace Ramsey\Uuid\Test\Encoder;
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit\Framework\MockObject\MockObject;
use Ramsey\Uuid\Builder\UuidBuilderInterface;
use Ramsey\Uuid\Codec\CodecInterface;
use Ramsey\Uuid\Codec\TimestampLastCombCodec;
@@ -16,7 +16,7 @@ class TimestampLastCombCodecTest extends TestCase
private $codec;
/**
* @var PHPUnit_Framework_MockObject_MockObject
* @var MockObject
*/
private $builderMock;
+1 -1
View File
@@ -73,7 +73,7 @@ class CombGeneratorTest extends TestCase
/**
* @dataProvider lengthLessThanSix
* @param $length
* @param int $length
* @throws \Exception
*/
public function testGenerateWithLessThanTimestampBytesThrowsException($length)
+12 -6
View File
@@ -2,33 +2,39 @@
namespace Ramsey\Uuid\Test\Generator;
use AspectMock\Test as AspectMock;
use Mockery;
use PHPUnit\Framework\MockObject\MockObject;
use Ramsey\Uuid\BinaryUtils;
use Ramsey\Uuid\Converter\TimeConverterInterface;
use Ramsey\Uuid\Generator\DefaultTimeGenerator;
use Ramsey\Uuid\Provider\NodeProviderInterface;
use Ramsey\Uuid\Provider\TimeProviderInterface;
use Ramsey\Uuid\Test\TestCase;
use Mockery;
use AspectMock\Test as AspectMock;
class DefaultTimeGeneratorTest extends TestCase
{
/** @var TimeProviderInterface */
/** @var TimeProviderInterface&MockObject */
private $timeProvider;
/** @var NodeProviderInterface */
/** @var NodeProviderInterface&MockObject */
private $nodeProvider;
/** @var TimeConverterInterface */
/** @var TimeConverterInterface&MockObject */
private $timeConverter;
/** @var string */
private $nodeId = '122f80ca9e06';
/** @var int[] */
private $currentTime;
/** @var string[] */
private $calculatedTime;
/** @var int */
private $clockSeq = 4066;
protected function setUp(): void
{
parent::setUp();
+1 -1
View File
@@ -25,7 +25,7 @@ class MtRandGeneratorTest extends TestCase
/**
* @dataProvider lengthDataProvider
* @param $length
* @param int $length
*/
public function testGenerateReturnsStringOfGivenLength($length)
{
+4 -4
View File
@@ -26,8 +26,8 @@ class OpenSslGeneratorTest extends TestCase
* @dataProvider lengthAndHexDataProvider
* @runInSeparateProcess
* @preserveGlobalState disabled
* @param $length
* @param $hex
* @param int $length
* @param string $hex
*/
public function testGenerateUsesOpenSsl($length, $hex)
{
@@ -43,8 +43,8 @@ class OpenSslGeneratorTest extends TestCase
* @dataProvider lengthAndHexDataProvider
* @runInSeparateProcess
* @preserveGlobalState disabled
* @param $length
* @param $hex
* @param int $length
* @param string $hex
*/
public function testGenerateReturnsRandomBytes($length, $hex)
{
@@ -39,11 +39,11 @@ class SodiumRandomGeneratorTest extends TestCase
$uuidFactory->setRandomGenerator(new SodiumRandomGenerator());
Uuid::setFactory($uuidFactory);
$uuid = Uuid::uuid4();
/** @var UuidFactory $actualUuidFactory */
$actualUuidFactory = Uuid::getFactory();
$this->assertInstanceOf(
SodiumRandomGenerator::class,
$uuid->getFactory()->getRandomGenerator()
$actualUuidFactory->getRandomGenerator()
);
}
}
@@ -267,8 +267,8 @@ class SystemNodeProviderTest extends TestCase
*
* @dataProvider provideCommandPerOs
*
* @param $os
* @param $command
* @param string $os
* @param string $command
*/
public function testGetNodeGetsNetworkInterfaceConfig($os, $command)
{
@@ -369,8 +369,8 @@ class SystemNodeProviderTest extends TestCase
*
* @dataProvider provideCommandPerOs
*
* @param $os
* @param $command
* @param string $os
* @param string $command
*/
public function testCallGetsysfsOnLinux($os, $command)
{
+27 -1
View File
@@ -123,6 +123,7 @@ class UuidTest extends TestCase
*/
public function testGetClockSeqHiAndReserved()
{
/** @var Uuid $uuid */
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals(155, $uuid->getClockSeqHiAndReserved());
}
@@ -131,6 +132,7 @@ class UuidTest extends TestCase
*/
public function testGetClockSeqHiAndReservedHex()
{
/** @var Uuid $uuid */
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals('9b', $uuid->getClockSeqHiAndReservedHex());
}
@@ -139,6 +141,7 @@ class UuidTest extends TestCase
*/
public function testGetClockSeqLow()
{
/** @var Uuid $uuid */
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals(33, $uuid->getClockSeqLow());
}
@@ -147,6 +150,7 @@ class UuidTest extends TestCase
*/
public function testGetClockSeqLowHex()
{
/** @var Uuid $uuid */
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals('21', $uuid->getClockSeqLowHex());
}
@@ -155,6 +159,7 @@ class UuidTest extends TestCase
*/
public function testGetClockSequence()
{
/** @var Uuid $uuid */
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals(6945, $uuid->getClockSequence());
}
@@ -163,6 +168,7 @@ class UuidTest extends TestCase
*/
public function testGetClockSequenceHex()
{
/** @var Uuid $uuid */
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals('1b21', $uuid->getClockSequenceHex());
}
@@ -260,6 +266,7 @@ class UuidTest extends TestCase
'node' => 8796630719078,
];
/** @var Uuid $uuid */
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals($fields, $uuid->getFields());
@@ -269,6 +276,7 @@ class UuidTest extends TestCase
{
Uuid::setFactory(new UuidFactory(new FeatureSet(false, true)));
/** @var Uuid $uuid */
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->expectException(UnsatisfiedDependencyException::class);
@@ -300,6 +308,7 @@ class UuidTest extends TestCase
{
$this->skipIfNoMoontoastMath();
/** @var Uuid $uuid */
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertInstanceOf('Moontoast\Math\BigNumber', $uuid->getLeastSignificantBits());
$this->assertEquals('11178224546741000806', $uuid->getLeastSignificantBits()->getValue());
@@ -309,6 +318,7 @@ class UuidTest extends TestCase
{
Uuid::setFactory(new UuidFactory(new FeatureSet(false, false, true)));
/** @var Uuid $uuid */
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->expectException(UnsatisfiedDependencyException::class);
@@ -330,6 +340,7 @@ class UuidTest extends TestCase
{
$this->skipIfNoMoontoastMath();
/** @var Uuid $uuid */
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertInstanceOf('Moontoast\Math\BigNumber', $uuid->getMostSignificantBits());
$this->assertEquals('18406084892941947361', $uuid->getMostSignificantBits()->getValue());
@@ -339,6 +350,7 @@ class UuidTest extends TestCase
{
Uuid::setFactory(new UuidFactory(new FeatureSet(false, false, true)));
/** @var Uuid $uuid */
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->expectException(UnsatisfiedDependencyException::class);
@@ -360,6 +372,7 @@ class UuidTest extends TestCase
{
$this->skip64BitTest();
/** @var Uuid $uuid */
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals(8796630719078, $uuid->getNode());
}
@@ -368,6 +381,7 @@ class UuidTest extends TestCase
{
Uuid::setFactory(new UuidFactory(new FeatureSet(false, true)));
/** @var Uuid $uuid */
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->expectException(UnsatisfiedDependencyException::class);
@@ -387,6 +401,7 @@ class UuidTest extends TestCase
*/
public function testGetTimeHiAndVersion()
{
/** @var Uuid $uuid */
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals(4577, $uuid->getTimeHiAndVersion());
}
@@ -405,6 +420,7 @@ class UuidTest extends TestCase
{
$this->skip64BitTest();
/** @var Uuid $uuid */
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals(4285500592, $uuid->getTimeLow());
}
@@ -413,6 +429,7 @@ class UuidTest extends TestCase
{
Uuid::setFactory(new UuidFactory(new FeatureSet(false, true)));
/** @var Uuid $uuid */
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->expectException(UnsatisfiedDependencyException::class);
@@ -432,6 +449,7 @@ class UuidTest extends TestCase
*/
public function testGetTimeMid()
{
/** @var Uuid $uuid */
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals(50557, $uuid->getTimeMid());
}
@@ -451,10 +469,12 @@ class UuidTest extends TestCase
$this->skip64BitTest();
// Check for a recent date
/** @var Uuid $uuid */
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals(135606608744910000, $uuid->getTimestamp());
// Check for an old date
/** @var Uuid $uuid */
$uuid = Uuid::fromString('0901e600-0154-1000-9b21-0800200c9a66');
$this->assertEquals(1460440000000, $uuid->getTimestamp());
}
@@ -475,6 +495,7 @@ class UuidTest extends TestCase
public function testGetTimestampFromNonVersion1Uuid()
{
// Using a version 4 UUID to test
/** @var Uuid $uuid */
$uuid = Uuid::fromString('bf17b594-41f2-474f-bf70-4c90220f75de');
$this->expectException(UnsupportedOperationException::class);
@@ -498,6 +519,7 @@ class UuidTest extends TestCase
{
Uuid::setFactory(new UuidFactory(new FeatureSet(false, true)));
/** @var Uuid $uuid */
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->expectException(UnsatisfiedDependencyException::class);
@@ -653,6 +675,7 @@ class UuidTest extends TestCase
{
$this->skip64BitTest();
/** @var Uuid $uuid */
$uuid = Uuid::uuid1(0x0800200c9a66, 0x1669);
$this->assertInstanceOf('\Ramsey\Uuid\Uuid', $uuid);
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
@@ -667,6 +690,7 @@ class UuidTest extends TestCase
*/
public function testUuid1WithHexadecimalNode()
{
/** @var Uuid $uuid */
$uuid = Uuid::uuid1('7160355e');
$this->assertInstanceOf('\Ramsey\Uuid\Uuid', $uuid);
@@ -684,6 +708,7 @@ class UuidTest extends TestCase
*/
public function testUuid1WithMixedCaseHexadecimalNode()
{
/** @var Uuid $uuid */
$uuid = Uuid::uuid1('71B0aD5e');
$this->assertInstanceOf('\Ramsey\Uuid\Uuid', $uuid);
@@ -701,6 +726,7 @@ class UuidTest extends TestCase
*/
public function testUuid1WithNodeAndClockSequence32Bit()
{
/** @var Uuid $uuid */
$uuid = Uuid::uuid1(0x7fffffff, 0x1669);
$this->assertInstanceOf('\Ramsey\Uuid\Uuid', $uuid);
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
@@ -973,7 +999,7 @@ class UuidTest extends TestCase
$this->assertEquals(-1, $uuid3->compareTo($uuid4));
$this->assertEquals(1, $uuid4->compareTo($uuid3));
$this->assertEquals(-1, $uuid5->compareTo($uuid3));
$this->assertEquals(1, $uuid3->compareto($uuid5));
$this->assertEquals(1, $uuid3->compareTo($uuid5));
}
public function testCompareToReturnsZeroWhenDifferentCases()
+2
View File
@@ -7,6 +7,7 @@ if (!function_exists('uuid_create')) {
*/
function uuid_create($type = 0)
{
return '';
}
}
@@ -17,6 +18,7 @@ if (!function_exists('uuid_parse')) {
*/
function uuid_parse($uuid)
{
return '';
}
}