Skip to content

Commit

Permalink
Merge pull request #2 from dzhdmitry/psalm
Browse files Browse the repository at this point in the history
Psalm
  • Loading branch information
dzhdmitry authored Sep 12, 2021
2 parents c6397cd + 5c94f49 commit 1ac471f
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 18 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/php.yml → .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
name: Build
name: Codecov

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
push: ~
pull_request: ~

jobs:
build:
Expand Down
36 changes: 36 additions & 0 deletions .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Psalm

on:
push: ~
pull_request: ~

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Psalm
run: |
./vendor/bin/psalm --no-progress || ./vendor/bin/psalm --output-format=github --no-progress
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ use Dzhdmitry\TinkoffInvestApi\Streaming\WebsocketConnectionFactory;

while ($message = yield $connection->receive()) {
/** @var \Amp\Websocket\Message $message полученное из WebSocket сообщение */
/** @var AbstractResponse $response десериализованное тело сообщения */
/** @var AbstractResponse $response десериализованное тело сообщения */
$response = $deserializer->deserialize(yield $message->buffer());

echo $response->getEvent() . ' at ' . $response->getTime()->format(DATE_RFC3339) . "\n";
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "^9.3"
"phpunit/phpunit": "^9.3",
"vimeo/psalm": "^4.9"
}
}
16 changes: 16 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<psalm
errorLevel="3"
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"
cacheDirectory="./var/psalm/cache/"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
10 changes: 3 additions & 7 deletions src/Rest/Schema/Payload/MoneyAmount.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ class MoneyAmount

/**
* @param string $currency
* @param float $value
* @param $value
*/
public function __construct(string $currency, $value)
{
$this->currency = $currency;
$this->value = $value;
$this->value = (float) $value;
}

/**
Expand All @@ -45,10 +45,6 @@ public function getValue(): float
*/
public function __toString(): string
{
return sprintf(
is_float($this->value) ? '%.2f %s' : '%d %s',
$this->value,
$this->currency
);
return sprintf('%.2f %s', $this->value, $this->currency);
}
}
5 changes: 3 additions & 2 deletions src/Streaming/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Amp\Websocket\ClientMetadata;
use Amp\Websocket\ClosedException;
use Amp\Websocket\Code;
use Amp\Websocket\Message;
use Amp\Websocket\Options;
use Dzhdmitry\TinkoffInvestApi\Streaming\Schema\Request\RequestInterface;

Expand Down Expand Up @@ -54,7 +55,7 @@ public function unsubscribe(RequestInterface $request): Promise
}

/**
* @return Promise
* @return Promise<Message|null>
*
* @throws ClosedException
*/
Expand All @@ -67,7 +68,7 @@ public function receive(): Promise
* @param int $code
* @param string $reason
*
* @return Promise
* @return Promise<array>
*/
public function close(int $code = Code::NORMAL_CLOSE, string $reason = ''): Promise
{
Expand Down
9 changes: 7 additions & 2 deletions src/Streaming/Denormalizer/ResponseDenormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerAwareInterface;
use Symfony\Component\Serializer\SerializerAwareTrait;

/**
* Десериализует ассоциативныймассив в один из объектов, унаследованных от AbstractResponse.
* Десериализует ассоциативный массив в один из объектов, унаследованных от AbstractResponse.
* Класс обекта определяет по полю "event" массива
*/
class ResponseDenormalizer implements ContextAwareDenormalizerInterface, SerializerAwareInterface
Expand All @@ -25,7 +26,7 @@ class ResponseDenormalizer implements ContextAwareDenormalizerInterface, Seriali
/**
* @param string[] $responseTypes
*/
public function __construct($responseTypes)
public function __construct(array $responseTypes)
{
$this->responseTypes = $responseTypes;
}
Expand Down Expand Up @@ -53,6 +54,10 @@ public function denormalize($data, string $type, string $format = null, array $c
throw new UnexpectedValueException('Data has unknown event type ' . $data['event']);
}

if (!($this->serializer instanceof Serializer)) {
throw new \LogicException('Serializer must be instance of ' . Serializer::class);
}

return $this->serializer->denormalize($data, $this->responseTypes[$data['event']], $format, $context);
}
}

0 comments on commit 1ac471f

Please sign in to comment.