Skip to content

Commit

Permalink
chore: add and fix phpstan (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
gbprod authored Aug 19, 2020
1 parent 81e2924 commit 57a0a50
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 19 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ before_script:

script:
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
- make analyse

after_success:
- wget https://scrutinizer-ci.com/ocular.phar
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: install test-unit test-coverage
.PHONY: install test-unit test-coverage analyse

PHP?=php
COMPOSER?=composer
Expand All @@ -16,3 +16,6 @@ vendor: composer.lock

composer.lock: composer.json
$(COMPOSER) update

analyse: install
$(PHP) vendor/bin/phpstan analyse --level max ./src ./tests
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
},
"require-dev": {
"phpunit/phpunit": "^8.0",
"symfony/property-access": "^3.4|^4.3|^5.0"
"symfony/property-access": "^3.4|^4.3|^5.0",
"phpstan/phpstan": "^0.12.37"
},
"license": "WTFPL",
"authors": [
{
"name": "GBProd",
"name": "gbprod",
"email": "contact@gb-prod.fr"
}
]
Expand Down
15 changes: 12 additions & 3 deletions src/UuidDenormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
class UuidDenormalizer implements DenormalizerInterface
{
/**
* @inheritdoc
* {@inheritdoc}
*
* @param array<mixed> $context
*
* @return UuidInterface|null
*
* @throws UnexpectedValueException
*/
public function denormalize($data, $type, $format = null, array $context = [])
{
Expand All @@ -33,14 +39,17 @@ public function denormalize($data, $type, $format = null, array $context = [])
}

/**
* @inheritdoc
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return (Uuid::class === $type || UuidInterface::class === $type);
}

private function isValid($data)
/**
* @param mixed $data
*/
private function isValid($data): bool
{
return $data === null
|| (is_string($data) && Uuid::isValid($data));
Expand Down
8 changes: 6 additions & 2 deletions src/UuidNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@
class UuidNormalizer implements NormalizerInterface
{
/**
* @inheritdoc
* {@inheritdoc}
*
* @param array<mixed> $context
*
* @return string
*/
public function normalize($object, $format = null, array $context = [])
{
return $object->toString();
}

/**
* @inheritdoc
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
Expand Down
17 changes: 9 additions & 8 deletions tests/Functionnal/DenormalizeUsingPhpDocTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

class DenormalizeUsingPhpDocTest extends TestCase
{
/** @var Serializer */
private $serializer;

public function setUp(): void
Expand All @@ -39,23 +40,23 @@ public function setUp(): void
public function testDenormalizeWithUuid(): void
{
$uuid = Uuid::uuid4();
$denormalized = $this->serializer->denormalize(
$denormalized = (array) $this->serializer->denormalize(
['uuid' => $uuid->toString()],
ClassWithUuidAttribute::class
);

$this->assertEquals($uuid, $denormalized->uuid);
$this->assertEquals($uuid, $denormalized['uuid']);
}

public function testDenormalizeWithUuidInterface(): void
{
$uuid = Uuid::uuid4();
$denormalized = $this->serializer->denormalize(
$denormalized = (array) $this->serializer->denormalize(
['uuid' => $uuid->toString()],
ClassWithUuidInterfaceAttribute::class
);

$this->assertEquals($uuid, $denormalized->uuid);
$this->assertEquals($uuid, $denormalized['uuid']);
}

public function testDenormalizeWithArrayOfUuid(): void
Expand All @@ -67,14 +68,14 @@ public function testDenormalizeWithArrayOfUuid(): void
Uuid::uuid5(Uuid::NAMESPACE_DNS, 'php.net'),
];

$denormalized = $this->serializer->denormalize(
$denormalized = (array) $this->serializer->denormalize(
['uuids' => array_map(function ($uuid) {
return $uuid->toString();
}, $uuids)],
ClassWithArrayOfUuidAttribute::class
);

$this->assertEquals($uuids, $denormalized->uuids);
$this->assertEquals($uuids, $denormalized['uuids']);
}

public function testDenormalizeWithArrayOfUuidInterface(): void
Expand All @@ -86,14 +87,14 @@ public function testDenormalizeWithArrayOfUuidInterface(): void
Uuid::uuid5(Uuid::NAMESPACE_DNS, 'php.net'),
];

$denormalized = $this->serializer->denormalize(
$denormalized = (array) $this->serializer->denormalize(
['uuids' => array_map(function ($uuid) {
return $uuid->toString();
}, $uuids)],
ClassWithArrayOfUuidInterfaceAttribute::class
);

$this->assertEquals($uuids, $denormalized->uuids);
$this->assertEquals($uuids, $denormalized['uuids']);
}
}

Expand Down
9 changes: 6 additions & 3 deletions tests/Functionnal/NomalizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
use GBProd\UuidNormalizer\UuidNormalizer;
use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

class NormalizeTest extends TestCase
{
/** @var Serializer */
private $serializer;

public function setUp(): void
Expand All @@ -28,7 +30,7 @@ public function testNormalizeOnArray(): void
{
$uuid = Uuid::uuid4();

$normalized = $this->serializer->normalize(
$normalized = (array) $this->serializer->normalize(
['uuid' => $uuid]
);

Expand All @@ -43,7 +45,7 @@ public function testNormalizeOnObject(): void
$object = new ClassWithUuid();
$object->uuid = Uuid::uuid4();

$normalized = $this->serializer->normalize($object);
$normalized = (array) $this->serializer->normalize($object);

$this->assertEquals(
$object->uuid->toString(),
Expand All @@ -60,7 +62,7 @@ public function testNormalizeArrayOfUuidOnObject(): void
Uuid::uuid4(),
];

$normalized = $this->serializer->normalize($object);
$normalized = (array) $this->serializer->normalize($object);

$this->assertEquals(
$object->uuid[0]->toString(),
Expand All @@ -71,5 +73,6 @@ public function testNormalizeArrayOfUuidOnObject(): void

class ClassWithUuid
{
/** @var UuidInterface|array<UuidInterface> */
public $uuid;
}
4 changes: 4 additions & 0 deletions tests/UuidNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class UuidNormalizerTest extends TestCase
{
const UUID_SAMPLE = '110e8400-e29b-11d4-a716-446655440000';

/** @var UuidNormalizer */
private $normalizer;

public function setUp(): void
Expand All @@ -42,6 +43,9 @@ public function testSupportsIsTrueIfUuid(UuidInterface $uuid): void
);
}

/**
* @return array<array<UuidInterface>>
*/
public function uuidProvider(): array
{
return array(
Expand Down

0 comments on commit 57a0a50

Please sign in to comment.