Skip to content

Commit

Permalink
ci: add type checking (psalm) (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
eKsiK authored Jul 12, 2021
1 parent 96a52b8 commit 464ee94
Show file tree
Hide file tree
Showing 20 changed files with 152 additions and 33 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ jobs:
- name: Run friendsofphp/php-cs-fixer
run: composer php-cs

type-checker:
name: Type Checker

runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.0
coverage: none

- name: Install composer dependencies
uses: ramsey/composer-install@v1

- name: Run vimeo/psalm
run: composer psalm

tests:
name: Tests

Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"php-http/mock-client": "^1.4",
"phpunit/phpunit": "^8.5",
"symfony/http-client": "^5.3",
"symfony/var-dumper": "^5.3"
"symfony/var-dumper": "^5.3",
"vimeo/psalm": "^4.8"
},
"config": {
"sort-packages": true
Expand All @@ -41,6 +42,7 @@
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"psalm": "@php vendor/bin/psalm --no-progress",
"php-cs": "@php vendor/bin/php-cs-fixer fix --dry-run",
"php-cs-fix": "@php vendor/bin/php-cs-fixer fix",
"test": "@php vendor/bin/phpunit --configuration phpunit.dist.xml",
Expand Down
37 changes: 37 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0"?>
<psalm
errorLevel="5"
resolveFromConfigFile="true"
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"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
<issueHandlers>
<MissingConstructor>
<errorLevel type="suppress">
<directory name="src/Common/Dto"/>
<directory name="src/Common/ValueObject"/>
<directory name="src/Confirmation/ValueObject"/>
<directory name="src/Itn/ValueObject"/>
<directory name="src/Itn/Dto"/>
<directory name="src/PaywayList/ValueObject"/>
<directory name="src/PaywayList/Dto"/>
<directory name="src/RegulationList/ValueObject"/>
<directory name="src/RegulationList/Dto"/>
<directory name="src/Transaction/ValueObject"/>
<directory name="src/Transaction/Dto"/>
</errorLevel>
</MissingConstructor>
<UndefinedMethod>
<errorLevel type="info">
<file name="src/Common/Builder/ServiceDtoBuilder.php"/>
</errorLevel>
</UndefinedMethod>
</issueHandlers>
</psalm>
10 changes: 4 additions & 6 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace BlueMedia;

use BlueMedia\Common\ValueObject\HashableInterface;
use BlueMedia\Confirmation\Builder\ConfirmationVOBuilder;
use BlueMedia\HttpClient\HttpClientInterface;
use BlueMedia\Hash\HashChecker;
use BlueMedia\Itn\Builder\ItnVOBuilder;
use BlueMedia\Itn\Decoder\ItnDecoder;
use BlueMedia\Serializer\SerializableInterface;
use BlueMedia\Transaction\View;
use BlueMedia\Itn\ValueObject\Itn;
use BlueMedia\HttpClient\HttpClient;
Expand Down Expand Up @@ -40,7 +40,7 @@ final class Client
private $configuration;

/**
* @var HttpClientInterface|null
* @var HttpClientInterface
*/
private $httpClient;

Expand Down Expand Up @@ -126,11 +126,9 @@ public function doItnIn(string $itn): Response
/**
* Returns response for ITN IN request.
*
* @return Response
*
* @api
*/
public function doItnInResponse(Itn $itn, bool $transactionConfirmed = true)
public function doItnInResponse(Itn $itn, bool $transactionConfirmed = true): Response
{
return new Response(ItnResponseBuilder::build($itn, $transactionConfirmed, $this->configuration));
}
Expand Down Expand Up @@ -196,7 +194,7 @@ public function getRegulationList(string $gatewayUrl): Response
*
* @api
*/
public function checkHash(SerializableInterface $data): bool
public function checkHash(HashableInterface $data): bool
{
return HashChecker::checkHash($data, $this->configuration);
}
Expand Down
6 changes: 6 additions & 0 deletions src/Common/Builder/ServiceDtoBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

final class ServiceDtoBuilder
{
/**
* @template T of AbstractDto
* @psalm-param class-string<T> $type
*
* @return T
*/
public static function build(array $data, string $type, Configuration $configuration): AbstractDto
{
$serializer = new Serializer();
Expand Down
4 changes: 2 additions & 2 deletions src/Common/Dto/AbstractDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace BlueMedia\Common\Dto;

use BlueMedia\Common\ValueObject\AbstractValueObject;
use BlueMedia\HttpClient\ValueObject\Request;
use BlueMedia\Serializer\SerializableInterface;
use JMS\Serializer\Annotation\Type;

abstract class AbstractDto
Expand Down Expand Up @@ -39,5 +39,5 @@ public function getRequest(): ?Request
return $this->request;
}

abstract public function getRequestData(): SerializableInterface;
abstract public function getRequestData(): AbstractValueObject;
}
6 changes: 6 additions & 0 deletions src/Common/Parser/ServiceResponseParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@

final class ServiceResponseParser extends ResponseParser
{
/**
* @template T
* @psalm-param class-string<T> $type
*
* @return T
*/
public function parseListResponse(string $type): SerializableInterface
{
$this->isErrorResponse();
Expand Down
10 changes: 10 additions & 0 deletions src/Common/ValueObject/HashableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace BlueMedia\Common\ValueObject;

interface HashableInterface
{
public function toArray(): array;

public function getHash(): string;
}
3 changes: 2 additions & 1 deletion src/Confirmation/ValueObject/Confirmation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace BlueMedia\Confirmation\ValueObject;

use BlueMedia\Common\ValueObject\HashableInterface;
use JMS\Serializer\Annotation\Type;
use JMS\Serializer\Annotation\AccessorOrder;
use BlueMedia\Serializer\SerializableInterface;
Expand All @@ -17,7 +18,7 @@
* "hash"
* })
*/
class Confirmation extends AbstractValueObject implements SerializableInterface
class Confirmation extends AbstractValueObject implements SerializableInterface, HashableInterface
{
/**
* @var string
Expand Down
4 changes: 2 additions & 2 deletions src/Hash/HashChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace BlueMedia\Hash;

use BlueMedia\Common\ValueObject\HashableInterface;
use BlueMedia\Configuration;
use BlueMedia\Serializer\SerializableInterface;

final class HashChecker
{
public static function checkHash(SerializableInterface $data, Configuration $configuration): bool
public static function checkHash(HashableInterface $data, Configuration $configuration): bool
{
$dataHash = HashGenerator::generateHash(
$data->toArray(),
Expand Down
4 changes: 2 additions & 2 deletions src/Itn/Builder/ItnDtoBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
namespace BlueMedia\Itn\Builder;

use BlueMedia\Itn\Dto\ItnDto;
use BlueMedia\Serializer\SerializableInterface;
use BlueMedia\Serializer\Serializer;
use BlueMedia\Common\Util\XMLParser;

final class ItnDtoBuilder
{
public static function build(string $itnData): SerializableInterface
public static function build(string $itnData): ItnDto
{
$serializer = new Serializer();
$xmlData = XMLParser::parse($itnData);

$xmlTransaction = $xmlData->transactions->transaction->asXML();
/** @var ItnDto $itnDto */
$itnDto = $serializer->deserializeXml($xmlTransaction, ItnDto::class);

$itnDto->getItn()->setServiceID((string) $xmlData->serviceID);
Expand Down
3 changes: 2 additions & 1 deletion src/Itn/Dto/ItnDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace BlueMedia\Itn\Dto;

use BlueMedia\Common\Dto\AbstractDto;
use BlueMedia\Common\ValueObject\AbstractValueObject;
use BlueMedia\Itn\ValueObject\Itn;
use BlueMedia\Serializer\SerializableInterface;
use JMS\Serializer\Annotation\XmlList;
Expand All @@ -24,7 +25,7 @@ public function getItn(): Itn
return $this->itn;
}

public function getRequestData(): SerializableInterface
public function getRequestData(): AbstractValueObject
{
return $this->getItn();
}
Expand Down
4 changes: 2 additions & 2 deletions src/PaywayList/Dto/PaywayListDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace BlueMedia\PaywayList\Dto;

use BlueMedia\Common\Dto\AbstractDto;
use BlueMedia\Common\ValueObject\AbstractValueObject;
use JMS\Serializer\Annotation\Type;
use BlueMedia\Serializer\SerializableInterface;
use BlueMedia\PaywayList\ValueObject\PaywayList;

final class PaywayListDto extends AbstractDto
Expand All @@ -22,7 +22,7 @@ public function getPaywayList(): PaywayList
return $this->paywayList;
}

public function getRequestData(): SerializableInterface
public function getRequestData(): AbstractValueObject
{
return $this->getPaywayList();
}
Expand Down
4 changes: 2 additions & 2 deletions src/RegulationList/Dto/RegulationListDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace BlueMedia\RegulationList\Dto;

use BlueMedia\Common\ValueObject\AbstractValueObject;
use JMS\Serializer\Annotation\Type;
use BlueMedia\Common\Dto\AbstractDto;
use BlueMedia\Serializer\SerializableInterface;
use BlueMedia\RegulationList\ValueObject\RegulationList;

final class RegulationListDto extends AbstractDto
Expand All @@ -22,7 +22,7 @@ public function getRegulationList(): RegulationList
return $this->regulationList;
}

public function getRequestData(): SerializableInterface
public function getRequestData(): AbstractValueObject
{
return $this->getRegulationList();
}
Expand Down
26 changes: 23 additions & 3 deletions src/Serializer/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace BlueMedia\Serializer;

use BlueMedia\Common\Dto\AbstractDto;
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
Expand All @@ -13,6 +12,9 @@ final class Serializer implements SerializerInterface
{
private const XML_TYPE = 'xml';

/**
* @var \JMS\Serializer\Serializer
*/
private $serializer;

public function __construct()
Expand All @@ -26,7 +28,13 @@ public function __construct()
->build();
}

public function serializeDataToDto(array $data, string $type): AbstractDto
/**
* @template T
* @psalm-param class-string<T> $type
*
* @return T
*/
public function serializeDataToDto(array $data, string $type)
{
return $this->serializer->fromArray($data, $type);
}
Expand All @@ -36,12 +44,24 @@ public function toArray(object $object): array
return $this->serializer->toArray($object);
}

/**
* @template T
* @psalm-param class-string<T> $type
*
* @return T
*/
public function fromArray(array $data, string $type)
{
return $this->serializer->fromArray($data, $type);
}

public function deserializeXml(string $xml, string $type): SerializableInterface
/**
* @template T
* @psalm-param class-string<T> $type
*
* @return T
*/
public function deserializeXml(string $xml, string $type)
{
return $this->serializer->deserialize($xml, $type, self::XML_TYPE);
}
Expand Down
24 changes: 20 additions & 4 deletions src/Serializer/SerializerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@

namespace BlueMedia\Serializer;

use BlueMedia\Common\Dto\AbstractDto;

interface SerializerInterface
{
public function serializeDataToDto(array $data, string $type): AbstractDto;
/**
* @template T
* @psalm-param class-string<T> $type
*
* @return T
*/
public function serializeDataToDto(array $data, string $type);

public function toArray(object $object): array;

/**
* @template T
* @psalm-param class-string<T> $type
*
* @return T
*/
public function fromArray(array $data, string $type);

public function deserializeXml(string $xml, string $type): SerializableInterface;
/**
* @template T
* @psalm-param class-string<T> $type
*
* @return T
*/
public function deserializeXml(string $xml, string $type);

public function toXml($data): string;
}
Loading

0 comments on commit 464ee94

Please sign in to comment.