diff --git a/.travis.yml b/.travis.yml index dee5a35..4752b8c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/composer.json b/composer.json index c5cadb7..63b4f03 100644 --- a/composer.json +++ b/composer.json @@ -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" ] }, diff --git a/phpstan-tests.neon b/phpstan-tests.neon new file mode 100644 index 0000000..906234e --- /dev/null +++ b/phpstan-tests.neon @@ -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.#' diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..6eedbef --- /dev/null +++ b/phpstan.neon @@ -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#' diff --git a/src/BinaryUtils.php b/src/BinaryUtils.php index 18ea467..c27daa6 100644 --- a/src/BinaryUtils.php +++ b/src/BinaryUtils.php @@ -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 */ diff --git a/src/Converter/Time/PhpTimeConverter.php b/src/Converter/Time/PhpTimeConverter.php index 57c882d..4c089fa 100644 --- a/src/Converter/Time/PhpTimeConverter.php +++ b/src/Converter/Time/PhpTimeConverter.php @@ -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), diff --git a/src/FeatureSet.php b/src/FeatureSet.php index 2027b9e..e2203d0 100644 --- a/src/FeatureSet.php +++ b/src/FeatureSet.php @@ -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() { diff --git a/src/Provider/Time/FixedTimeProvider.php b/src/Provider/Time/FixedTimeProvider.php index 79a9d04..02b36d7 100644 --- a/src/Provider/Time/FixedTimeProvider.php +++ b/src/Provider/Time/FixedTimeProvider.php @@ -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) diff --git a/src/Uuid.php b/src/Uuid.php index 7ffcec4..6c6d597 100644 --- a/src/Uuid.php +++ b/src/Uuid.php @@ -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; diff --git a/tests/Codec/GuidStringCodecTest.php b/tests/Codec/GuidStringCodecTest.php index 0fdbe4a..3488ead 100644 --- a/tests/Codec/GuidStringCodecTest.php +++ b/tests/Codec/GuidStringCodecTest.php @@ -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; diff --git a/tests/Codec/OrderedTimeCodecTest.php b/tests/Codec/OrderedTimeCodecTest.php index c3e4d58..b2c178d 100644 --- a/tests/Codec/OrderedTimeCodecTest.php +++ b/tests/Codec/OrderedTimeCodecTest.php @@ -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'; diff --git a/tests/Codec/StringCodecTest.php b/tests/Codec/StringCodecTest.php index 6af432d..b00220f 100644 --- a/tests/Codec/StringCodecTest.php +++ b/tests/Codec/StringCodecTest.php @@ -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'; diff --git a/tests/Converter/Time/PhpTimeConverterTest.php b/tests/Converter/Time/PhpTimeConverterTest.php index 5574f34..0fe25a5 100644 --- a/tests/Converter/Time/PhpTimeConverterTest.php +++ b/tests/Converter/Time/PhpTimeConverterTest.php @@ -1,6 +1,6 @@ setRandomGenerator(new SodiumRandomGenerator()); Uuid::setFactory($uuidFactory); - $uuid = Uuid::uuid4(); - + /** @var UuidFactory $actualUuidFactory */ + $actualUuidFactory = Uuid::getFactory(); $this->assertInstanceOf( SodiumRandomGenerator::class, - $uuid->getFactory()->getRandomGenerator() + $actualUuidFactory->getRandomGenerator() ); } } diff --git a/tests/Provider/Node/SystemNodeProviderTest.php b/tests/Provider/Node/SystemNodeProviderTest.php index 7569d43..e71caa1 100644 --- a/tests/Provider/Node/SystemNodeProviderTest.php +++ b/tests/Provider/Node/SystemNodeProviderTest.php @@ -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) { diff --git a/tests/UuidTest.php b/tests/UuidTest.php index 2335a20..b875de2 100644 --- a/tests/UuidTest.php +++ b/tests/UuidTest.php @@ -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() diff --git a/tests/phpstan-bootstrap.php b/tests/phpstan-bootstrap.php index c7f0535..8c3db30 100644 --- a/tests/phpstan-bootstrap.php +++ b/tests/phpstan-bootstrap.php @@ -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 ''; } }