From 277575d686241666a7f72f5dcd0b6823c01b94ef Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Tue, 29 Oct 2019 22:08:11 -0700 Subject: [PATCH] Use PHPStan in Travis CI --- .editorconfig | 6 ++++++ .travis.yml | 3 ++- Makefile | 22 +++++++++++++++++++++ lib/ApiRequestor.php | 2 +- lib/Customer.php | 2 +- lib/HttpClient/CurlClient.php | 7 +++++-- lib/StripeObject.php | 2 +- phpstan-baseline.neon | 15 ++++++++++++++ phpstan.neon | 8 ++++++++ tests/Stripe/CollectionTest.php | 3 +++ tests/Stripe/HttpClient/CurlClientTest.php | 23 +++++++++++++++++++++- tests/Stripe/StripeObjectTest.php | 6 ++++++ tests/Stripe/StripeTest.php | 3 +++ tests/TestCase.php | 2 +- 14 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 Makefile create mode 100644 phpstan-baseline.neon create mode 100644 phpstan.neon diff --git a/.editorconfig b/.editorconfig index 9e93e0c4c3..174e9120bf 100644 --- a/.editorconfig +++ b/.editorconfig @@ -12,3 +12,9 @@ charset = utf-8 [*.{json,yml}] indent_size = 2 + +[*.neon] +indent_style = tab + +[Makefile] +indent_style = tab diff --git a/.travis.yml b/.travis.yml index e0995fc071..13cb8196a2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -51,7 +51,7 @@ cache: before_install: # Install various build dependencies. We use `travis_retry` because Composer # will occasionally fail intermittently. - - travis_retry composer install + - travis_retry make vendor # Unpack and start stripe-mock so that the test suite can talk to it - | @@ -68,5 +68,6 @@ before_install: script: - ./build.php ${AUTOLOAD} - ./vendor/bin/php-cs-fixer fix -v --dry-run --using-cache=no . + - if [[ `php -r 'echo \version_compare(PHP_VERSION, "7.1") >= 0 ? "1" : "0";'` -gt 0 && $AUTOLOAD == 1 ]]; then make phpstan; fi after_script: ./vendor/bin/coveralls -v diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000..8627cc4b13 --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +export PHPSTAN_VERSION := 0.11.19 + +vendor: composer.json + composer install + curl -sfL https://github.com/phpstan/phpstan/releases/download/$(PHPSTAN_VERSION)/phpstan.phar -o vendor/bin/phpstan + chmod +x vendor/bin/phpstan + +test: vendor + vendor/bin/phpunit +.PHONY: test + +fmt: vendor + vendor/bin/php-cs-fixer fix -v --using-cache=no . +.PHONY: fmt + +phpstan: vendor + vendor/bin/phpstan analyse -c phpstan.neon lib tests +.PHONY: phpstan + +phpstan-baseline: vendor + vendor/bin/phpstan analyse -c phpstan.neon --error-format baselineNeon lib tests > phpstan-baseline.neon +.PHONY: phpstan-baseline diff --git a/lib/ApiRequestor.php b/lib/ApiRequestor.php index 32b3a21602..80e28f0772 100644 --- a/lib/ApiRequestor.php +++ b/lib/ApiRequestor.php @@ -129,7 +129,7 @@ public function handleErrorResponse($rbody, $rcode, $rheaders, $resp) if (!is_array($resp) || !isset($resp['error'])) { $msg = "Invalid response object from API: $rbody " . "(HTTP response code was $rcode)"; - throw new Exception\UnexpectedValueException($msg, $rcode, $rbody, $resp, $rheaders); + throw new Exception\UnexpectedValueException($msg); } $errorData = $resp['error']; diff --git a/lib/Customer.php b/lib/Customer.php index c6dcdb10eb..7ebb171977 100644 --- a/lib/Customer.php +++ b/lib/Customer.php @@ -22,7 +22,7 @@ * @property StripeObject $metadata * @property string $name * @property string $phone - * @property string[] preferred_locales + * @property string[] $preferred_locales * @property mixed $shipping * @property Collection $sources * @property Collection $subscriptions diff --git a/lib/HttpClient/CurlClient.php b/lib/HttpClient/CurlClient.php index 26568b247e..7e6e02a974 100644 --- a/lib/HttpClient/CurlClient.php +++ b/lib/HttpClient/CurlClient.php @@ -38,6 +38,9 @@ public static function instance() protected $defaultOptions; + /** @var \Stripe\Util\RandomGenerator */ + protected $randomGenerator; + protected $userAgentInfo; protected $enablePersistentConnections = true; @@ -380,7 +383,7 @@ private function handleCurlError($url, $errno, $message, $numRetries) * * @param int $errno * @param int $rcode - * @param array|CaseInsensitiveArray $rheaders + * @param array|\Stripe\Util\CaseInsensitiveArray $rheaders * @param int $numRetries * * @return bool @@ -435,7 +438,7 @@ private function shouldRetry($errno, $rcode, $rheaders, $numRetries) * Provides the number of seconds to wait before retrying a request. * * @param int $numRetries - * @param array|CaseInsensitiveArray $rheaders + * @param array|\Stripe\Util\CaseInsensitiveArray $rheaders * * @return int */ diff --git a/lib/StripeObject.php b/lib/StripeObject.php index 41c066c6fc..ca4bf3da7c 100644 --- a/lib/StripeObject.php +++ b/lib/StripeObject.php @@ -361,7 +361,7 @@ public function serializeParamsValue($value, $original, $unsaved, $force, $key = // user intended it to. if ($value === null) { return ""; - } elseif (($value instanceof APIResource) && (!$value->saveWithParent)) { + } elseif (($value instanceof ApiResource) && (!$value->saveWithParent)) { if (!$unsaved) { return null; } elseif (isset($value->id)) { diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 0000000000..2d465bc2ff --- /dev/null +++ b/phpstan-baseline.neon @@ -0,0 +1,15 @@ + + +parameters: + ignoreErrors: + - + message: "#^Access to undefined constant Stripe\\\\ApiResource\\:\\:OBJECT_NAME\\.$#" + count: 1 + path: lib/ApiResource.php + + - + message: "#^Access to undefined constant Stripe\\\\SingletonApiResource\\:\\:OBJECT_NAME\\.$#" + count: 1 + path: lib/SingletonApiResource.php + + diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000000..2f36812456 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,8 @@ +includes: + - phpstan-baseline.neon + +parameters: + level: 1 + bootstrap: tests/bootstrap.php + autoload_directories: + - tests diff --git a/tests/Stripe/CollectionTest.php b/tests/Stripe/CollectionTest.php index 7279fdfb94..5715412d96 100644 --- a/tests/Stripe/CollectionTest.php +++ b/tests/Stripe/CollectionTest.php @@ -4,6 +4,9 @@ class CollectionTest extends TestCase { + /** @var \Stripe\Collection */ + private $fixture; + /** * @before */ diff --git a/tests/Stripe/HttpClient/CurlClientTest.php b/tests/Stripe/HttpClient/CurlClientTest.php index 36f5c25255..c102ab47ec 100644 --- a/tests/Stripe/HttpClient/CurlClientTest.php +++ b/tests/Stripe/HttpClient/CurlClientTest.php @@ -6,6 +6,27 @@ class CurlClientTest extends TestCase { + /** @var \ReflectionProperty */ + private $initialNetworkRetryDelayProperty; + + /** @var \ReflectionProperty */ + private $maxNetworkRetryDelayProperty; + + /** @var double */ + private $origInitialNetworkRetryDelay; + + /** @var int */ + private $origMaxNetworkRetries; + + /** @var double */ + private $origMaxNetworkRetryDelay; + + /** @var \ReflectionMethod */ + private $sleepTimeMethod; + + /** @var \ReflectionMethod */ + private $shouldRetryMethod; + /** * @before */ @@ -60,7 +81,7 @@ private function setInitialNetworkRetryDelay($initialNetworkRetryDelay) private function createFakeRandomGenerator($returnValue = 1.0) { - $fakeRandomGenerator = $this->createMock('Stripe\Util\RandomGenerator', ['randFloat']); + $fakeRandomGenerator = $this->createMock('Stripe\Util\RandomGenerator'); $fakeRandomGenerator->method('randFloat')->willReturn($returnValue); return $fakeRandomGenerator; } diff --git a/tests/Stripe/StripeObjectTest.php b/tests/Stripe/StripeObjectTest.php index 25c3a0acfc..180b09002a 100644 --- a/tests/Stripe/StripeObjectTest.php +++ b/tests/Stripe/StripeObjectTest.php @@ -4,6 +4,12 @@ class StripeObjectTest extends TestCase { + /** @var \ReflectionMethod */ + private $deepCopyReflector; + + /** @var \ReflectionMethod */ + private $optsReflector; + /** * @before */ diff --git a/tests/Stripe/StripeTest.php b/tests/Stripe/StripeTest.php index f594518d13..d34bd4828a 100644 --- a/tests/Stripe/StripeTest.php +++ b/tests/Stripe/StripeTest.php @@ -4,6 +4,9 @@ class StripeTest extends TestCase { + /** @var array */ + protected $orig; + /** * @before */ diff --git a/tests/TestCase.php b/tests/TestCase.php index 2d62c0b2c2..850e4d7ff5 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -140,7 +140,7 @@ protected function stubRequest( * Defaults to false. * @param string|null $base base URL (e.g. 'https://api.stripe.com') * - * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker + * @return \PHPUnit_Framework_MockObject_Builder_InvocationMocker */ private function prepareRequestMock( $method,