From bdc9170edc081da556f7fc67af5889c0d187f9ed Mon Sep 17 00:00:00 2001 From: Barney Laurance Date: Sat, 28 Dec 2019 00:35:40 +0000 Subject: [PATCH] Run Psalm on src as well as Static Analysis Fixture (#282) * Run Psalm on src as well as Static Analysis Fixture Psalm baselining feature is used to prevent immediate build failures - psalm will just cause failures for errors caused by future changes to the code. See https://psalm.dev/docs/running_psalm/dealing_with_code_issues/#using-a-baseline-file * Fix MixedArgument issues in OrderedTimeCodec Potentially unpack should be documented as returning string[] instead of array to make this unecassary. * Fix Psalm PossiblyNullPropertyAssignmentValue issues * Add psalm-immutable annotation to Uuid class * Fix Psalm MixedArgument issues * Fix MissingImmutableAnnotation psalm issue * Fix Psalm MixedAssignment issue * Fix Psalm InvalidReturnStatement / InvalidReturnType issue * Add ImpureMethodCall issues to baseline. Fixing these in one place just moves them somewhere else. * Exclude psalm-baseline.xml from exported git archives --- .gitattributes | 1 + psalm-baseline.xml | 125 ++++++++++++++++++++++++ psalm.xml | 9 ++ src/BinaryUtils.php | 2 + src/Codec/GuidStringCodec.php | 3 + src/Codec/OrderedTimeCodec.php | 6 +- src/Converter/Time/PhpTimeConverter.php | 1 + src/DegradedUuid.php | 2 + src/Uuid.php | 2 + src/UuidFactory.php | 14 +-- 10 files changed, 157 insertions(+), 8 deletions(-) create mode 100644 psalm-baseline.xml diff --git a/.gitattributes b/.gitattributes index 8079d31..551ae95 100644 --- a/.gitattributes +++ b/.gitattributes @@ -7,6 +7,7 @@ phpcs.xml.dist export-ignore phpstan-tests.neon export-ignore phpstan.neon export-ignore phpunit.xml.dist export-ignore +psalm-baseline.xml export-ignore resources/ export-ignore static-analysis/ export-ignore tests/ export-ignore diff --git a/psalm-baseline.xml b/psalm-baseline.xml new file mode 100644 index 0000000..fcfa11b --- /dev/null +++ b/psalm-baseline.xml @@ -0,0 +1,125 @@ + + + + + $hexUuid[1] + + + + + fromHex + convertTime + + + $exception->getCode() + + + + + $exception->getCode() + + + $node + + + + + $uuid + + + string + + + uuid_parse($uuid) + + + uuid_create(UUID_TYPE_RANDOM) + uuid_parse($uuid) + + + + + $uuid + + + string + + + uuid_parse($uuid) + + + uuid_create(UUID_TYPE_TIME) + uuid_parse($uuid) + + + + + $exception->getCode() + + + + + $exception->getCode() + hexdec(bin2hex($nodeMsb)) | 0x010000 + + + + + $addressPath + + + $node + constant('PHP_OS') + constant('PHP_OS') + $addressPath + $addressPath + $macs + + + $node + $node + + + getNode + + + is_array($node) + + + + + self::$factory + + + encodeBinary + convertTime + fromHex + fromHex + fromHex + encode + getFactory + fromBytes + getFactory + fromString + getFactory + fromInteger + getFactory + getValidator + validate + + + $exception->getCode() + + + null + + + + + $hash + + + $hash + + + diff --git a/psalm.xml b/psalm.xml index 7abdda9..d3e6c20 100644 --- a/psalm.xml +++ b/psalm.xml @@ -5,8 +5,17 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://getpsalm.org/schema/config" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" + errorBaseline="psalm-baseline.xml" > + + + diff --git a/src/BinaryUtils.php b/src/BinaryUtils.php index 27d22c0..e5132a2 100644 --- a/src/BinaryUtils.php +++ b/src/BinaryUtils.php @@ -54,6 +54,8 @@ class BinaryUtils $timeHi = hexdec($timeHi) & 0x0fff; $timeHi |= $version << 12; + assert(\is_int($timeHi)); + return $timeHi; } } diff --git a/src/Codec/GuidStringCodec.php b/src/Codec/GuidStringCodec.php index 8dea903..ea51128 100644 --- a/src/Codec/GuidStringCodec.php +++ b/src/Codec/GuidStringCodec.php @@ -78,12 +78,15 @@ class GuidStringCodec extends StringCodec private function swapFields(array &$components): void { $hex = unpack('H*', pack('L', hexdec($components[0]))); + assert(is_string($hex[1])); $components[0] = $hex[1]; $hex = unpack('H*', pack('S', hexdec($components[1]))); + assert(is_string($hex[1])); $components[1] = $hex[1]; $hex = unpack('H*', pack('S', hexdec($components[2]))); + assert(is_string($hex[1])); $components[2] = $hex[1]; } } diff --git a/src/Codec/OrderedTimeCodec.php b/src/Codec/OrderedTimeCodec.php index ea26fac..bc2af0c 100644 --- a/src/Codec/OrderedTimeCodec.php +++ b/src/Codec/OrderedTimeCodec.php @@ -75,7 +75,11 @@ class OrderedTimeCodec extends StringCodec ); } - $hex = unpack('H*', $bytes)[1]; + $unpacked = unpack('H*', $bytes); + + assert(is_string($unpacked[1])); + + $hex = $unpacked[1]; // Rearrange the fields to their original order $hex = substr($hex, 8, 4) diff --git a/src/Converter/Time/PhpTimeConverter.php b/src/Converter/Time/PhpTimeConverter.php index 4b7bfb4..f92ae82 100644 --- a/src/Converter/Time/PhpTimeConverter.php +++ b/src/Converter/Time/PhpTimeConverter.php @@ -46,6 +46,7 @@ class PhpTimeConverter implements TimeConverterInterface // UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00. $uuidTime = ((int) $seconds * 10000000) + ((int) $microSeconds * 10) + 0x01b21dd213814000; + /** @psalm-suppress MixedArgument*/ return [ 'low' => sprintf('%08x', $uuidTime & 0xffffffff), 'mid' => sprintf('%04x', ($uuidTime >> 32) & 0xffff), diff --git a/src/DegradedUuid.php b/src/DegradedUuid.php index 4a66431..d82ad0c 100644 --- a/src/DegradedUuid.php +++ b/src/DegradedUuid.php @@ -26,6 +26,8 @@ use Ramsey\Uuid\Exception\UnsupportedOperationException; * Some of the functionality of a DegradedUuid is not present or degraded, since * 32-bit systems are unable to perform the necessary mathematical operations or * represent the integers appropriately. + * + * @psalm-immutable */ class DegradedUuid extends Uuid { diff --git a/src/Uuid.php b/src/Uuid.php index 02a1864..76379bf 100644 --- a/src/Uuid.php +++ b/src/Uuid.php @@ -34,6 +34,8 @@ use Ramsey\Uuid\Exception\UnsupportedOperationException; * Note that `uuid1()` may compromise privacy since it creates a UUID containing * the computer’s network address. `uuid4()` creates a random UUID. * + * @psalm-immutable + * * @link http://tools.ietf.org/html/rfc4122 RFC 4122 */ class Uuid implements UuidInterface diff --git a/src/UuidFactory.php b/src/UuidFactory.php index ad35bf5..725c3f6 100644 --- a/src/UuidFactory.php +++ b/src/UuidFactory.php @@ -27,37 +27,37 @@ class UuidFactory implements UuidFactoryInterface /** * @var CodecInterface */ - private $codec = null; + private $codec; /** * @var NodeProviderInterface */ - private $nodeProvider = null; + private $nodeProvider; /** * @var NumberConverterInterface */ - private $numberConverter = null; + private $numberConverter; /** * @var RandomGeneratorInterface */ - private $randomGenerator = null; + private $randomGenerator; /** * @var TimeGeneratorInterface */ - private $timeGenerator = null; + private $timeGenerator; /** * @var UuidBuilderInterface */ - private $uuidBuilder = null; + private $uuidBuilder; /** * @var ValidatorInterface */ - private $validator = null; + private $validator; /** * @param FeatureSet $features A set of available features in the current environment