Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add php qa report #21

Merged
merged 4 commits into from Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
build/
vendor/
composer.lock
.php_cs.cache
1 change: 1 addition & 0 deletions .php_cs.dist
Expand Up @@ -32,6 +32,7 @@ return PhpCsFixer\Config::create()
'self_accessor' => false,
'yoda_style' => null,
'non_printable_character' => true,
'phpdoc_no_empty_return' => false,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes tools like psalm cry a river, sadly.

])
->setFinder($finder)
->setCacheFile(__DIR__.'/.php_cs.cache')
Expand Down
7 changes: 6 additions & 1 deletion .travis.yml
@@ -1,5 +1,8 @@
language: php

services:
- docker

notifications:
email:
on_success: never
Expand Down Expand Up @@ -32,6 +35,8 @@ jobs:
- stage: PHPStan Code quality
script:
- composer require --dev "phpstan/phpstan:^0.10" --ignore-platform-reqs && composer phpstan

- stage: PHPQA Full analysis
script:
- docker run --rm -u $UID -v $PWD:/app eko3alpha/docker-phpqa --report --ignoredDirs vendor
after_success:
- bash <(curl -s https://codecov.io/bash)
6 changes: 3 additions & 3 deletions src/Clients/GuzzleClient.php
Expand Up @@ -2,10 +2,10 @@

namespace PrestaShop\CircuitBreaker\Clients;

use PrestaShop\CircuitBreaker\Exceptions\UnavailableService;
use PrestaShop\CircuitBreaker\Contracts\Client;
use GuzzleHttp\Client as OriginalGuzzleClient;
use Exception;
use GuzzleHttp\Client as OriginalGuzzleClient;
use PrestaShop\CircuitBreaker\Contracts\Client;
use PrestaShop\CircuitBreaker\Exceptions\UnavailableService;

/**
* Guzzle implementation of client.
Expand Down
12 changes: 8 additions & 4 deletions src/Contracts/CircuitBreaker.php
Expand Up @@ -10,7 +10,7 @@
interface CircuitBreaker
{
/**
* @var string the circuit breaker state
* @return string the circuit breaker state
*/
public function getState();

Expand All @@ -19,21 +19,25 @@ public function getState();
*
* @var string the function to call
* @var callable $fallback if the service is unavailable, rely on the fallback
*
* @return string
*
* @param mixed $service
*/
public function call($service, callable $fallback);

/**
* @var bool checks if the circuit breaker is open
* @return bool checks if the circuit breaker is open
*/
public function isOpened();

/**
* @var bool checks if the circuit breaker is half open
* @return bool checks if the circuit breaker is half open
*/
public function isHalfOpened();

/**
* @var bool checks if the circuit breaker is closed
* @return bool checks if the circuit breaker is closed
*/
public function isClosed();
}
6 changes: 4 additions & 2 deletions src/Contracts/Client.php
Expand Up @@ -9,8 +9,10 @@
interface Client
{
/**
* @var string The URI of the service to be reached
* @var array $options the options if needed
* @param string $resource the URI of the service to be reached
* @param array $options the options if needed
*
* @return string
*/
public function request($resource, array $options);
}
4 changes: 3 additions & 1 deletion src/Contracts/Factory.php
Expand Up @@ -8,7 +8,9 @@
interface Factory
{
/**
* @var array the settings for the Places
* @param array $settings the settings for the Places
*
* @return CircuitBreaker
*/
public function create(array $settings);
}
6 changes: 3 additions & 3 deletions src/Contracts/Place.php
Expand Up @@ -17,17 +17,17 @@ interface Place
public function getState();

/**
* @var int the number of failures
* @return int the number of failures
*/
public function getFailures();

/**
* @var int the allowed number of trials
* @return int the allowed number of trials
*/
public function getThreshold();

/**
* @var int the allowed timeout
* @return float the allowed timeout
*/
public function getTimeout();
}
6 changes: 6 additions & 0 deletions src/Contracts/Storage.php
Expand Up @@ -17,6 +17,8 @@ interface Storage
* @var Transaction $transaction the transaction
*
* @return bool
*
* @param mixed $service
*/
public function saveTransaction($service, Transaction $transaction);

Expand All @@ -28,6 +30,8 @@ public function saveTransaction($service, Transaction $transaction);
* @throws TransactionNotFound
*
* @return Transaction
*
* @param mixed $service
*/
public function getTransaction($service);

Expand All @@ -37,6 +41,8 @@ public function getTransaction($service);
* @var string the service name
*
* @return bool
*
* @param mixed $service
*/
public function hasTransaction($service);

Expand Down
10 changes: 5 additions & 5 deletions src/Contracts/Transaction.php
Expand Up @@ -11,23 +11,23 @@
interface Transaction
{
/**
* @var string the service name
* @return string the service name
*/
public function getService();

/**
* @var int the number of failures to call the service
* @return int the number of failures to call the service
*/
public function getFailures();

/**
* @var string the current state of the Circuit Breaker
* @return string the current state of the Circuit Breaker
*/
public function getState();

/**
* @var DateTime the time when the circuit breaker move
* from open to half open state
* @return DateTime the time when the circuit breaker move
* from open to half open state
*/
public function getThresholdDateTime();

Expand Down
4 changes: 3 additions & 1 deletion src/Exceptions/InvalidPlace.php
Expand Up @@ -2,15 +2,17 @@

namespace PrestaShop\CircuitBreaker\Exceptions;

use PrestaShop\CircuitBreaker\Utils\ErrorFormatter;
use Exception;
use PrestaShop\CircuitBreaker\Utils\ErrorFormatter;

final class InvalidPlace extends Exception
{
/**
* @param mixed $failures the failures
* @param mixed $timeout the timeout
* @param mixed $threshold the threshold
*
* @return self
*/
public static function invalidSettings($failures, $timeout, $threshold)
{
Expand Down
4 changes: 3 additions & 1 deletion src/Exceptions/InvalidTransaction.php
Expand Up @@ -2,8 +2,8 @@

namespace PrestaShop\CircuitBreaker\Exceptions;

use PrestaShop\CircuitBreaker\Utils\ErrorFormatter;
use Exception;
use PrestaShop\CircuitBreaker\Utils\ErrorFormatter;

final class InvalidTransaction extends Exception
{
Expand All @@ -12,6 +12,8 @@ final class InvalidTransaction extends Exception
* @param mixed $failures the failures
* @param mixed $state the Circuit Breaker
* @param mixed $threshold the threshold
*
* @return self
*/
public static function invalidParameters($service, $failures, $state, $threshold)
{
Expand Down
67 changes: 42 additions & 25 deletions src/Places/AbstractPlace.php
Expand Up @@ -2,23 +2,39 @@

namespace PrestaShop\CircuitBreaker\Places;

use PrestaShop\CircuitBreaker\Exceptions\InvalidPlace;
use PrestaShop\CircuitBreaker\Contracts\Place;
use PrestaShop\CircuitBreaker\Exceptions\InvalidPlace;
use PrestaShop\CircuitBreaker\Utils\Assert;

abstract class AbstractPlace implements Place
{
/**
* @var int the Place failures
*/
private $failures;

/**
* @var float the Place timeout
*/
private $timeout;

/**
* @var int the Place threshold
*/
private $threshold;

/**
* @param int $failures the Place failures
* @param float $timeout the Place timeout
* @param int $threshold the Place threshold
*/
public function __construct($failures, $timeout, $threshold)
{
if ($this->validate($failures, $timeout, $threshold)) {
$this->failures = $failures;
$this->timeout = $timeout;
$this->threshold = $threshold;
}
$this->validate($failures, $timeout, $threshold);

$this->failures = $failures;
$this->timeout = $timeout;
$this->threshold = $threshold;
}

/**
Expand Down Expand Up @@ -51,10 +67,22 @@ public function getThreshold()
}

/**
* Ensure the place is valid (PHP5 is permissive)
* Helper: create a Place from an array.
*
* @var array the failures, timeout and treshold
*
* @return self
*/
public static function fromArray(array $settings)
{
return new static($settings[0], $settings[1], $settings[2]);
}

/**
* Ensure the place is valid (PHP5 is permissive).
*
* @param int $failures the failures should be a positive value
* @param int $timeout the timeout should be a positive value
* @param float $timeout the timeout should be a positive value
* @param int $threshold the threshold should be a positive value
*
* @throws InvalidPlace
Expand All @@ -63,26 +91,15 @@ public function getThreshold()
*/
private function validate($failures, $timeout, $threshold)
{
if (
Assert::isPositiveInteger($failures) &&
Assert::isPositiveValue($timeout) &&
Assert::isPositiveInteger($threshold)
) {
$assertionsAreValid = Assert::isPositiveInteger($failures)
&& Assert::isPositiveValue($timeout)
&& Assert::isPositiveInteger($threshold)
;

if ($assertionsAreValid) {
return true;
}

throw InvalidPlace::invalidSettings($failures, $timeout, $threshold);
}

/**
* Helper: create a Place from an array
*
* @var array the failures, timeout and treshold
*
* @return self
*/
public static function fromArray(array $settings)
{
return new static($settings[0], $settings[1], $settings[2]);
}
}