From 1d56061740f14f7e65b114a1e5901dba1e6b457d Mon Sep 17 00:00:00 2001 From: Sebastiaan Stok Date: Mon, 27 Nov 2023 14:01:33 +0100 Subject: [PATCH] Fix PHPStan errors --- phpstan-baseline.neon | 6 ++++++ phpstan.neon | 3 ++- src/OCSPValidator.php | 2 ++ src/Violation/UnsupportedDomain.php | 2 +- src/X509DataExtractor.php | 3 +++ src/X509Info.php | 4 +++- tests/CertificateValidatorTest.php | 8 ++++---- tests/OCSPValidatorTest.php | 22 +++------------------- 8 files changed, 24 insertions(+), 26 deletions(-) create mode 100644 phpstan-baseline.neon diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 0000000..ea1438a --- /dev/null +++ b/phpstan-baseline.neon @@ -0,0 +1,6 @@ +parameters: + ignoreErrors: + - + message: "#^Access to an undefined property Rollerworks\\\\Component\\\\X509Validator\\\\CertificateValidator\\:\\:\\$now\\.$#" + count: 1 + path: tests/CertificateValidatorTest.php diff --git a/phpstan.neon b/phpstan.neon index 0e56097..eb6d520 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,6 +1,6 @@ includes: - vendor/rollerscapes/standards/phpstan.neon - #- phpstan-baseline.neon + - phpstan-baseline.neon parameters: #reportUnmatchedIgnoredErrors: false @@ -12,5 +12,6 @@ parameters: - var/ - templates/ - translations/ + - tests/TestLogger.php #ignoreErrors: diff --git a/src/OCSPValidator.php b/src/OCSPValidator.php index de67fff..a02173e 100644 --- a/src/OCSPValidator.php +++ b/src/OCSPValidator.php @@ -20,6 +20,7 @@ use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; use Rollerworks\Component\X509Validator\Violation\CertificateIsRevoked; +use Rollerworks\Component\X509Validator\Violation\UnprocessablePEM; use Symfony\Component\HttpClient\HttpClient; use Symfony\Contracts\HttpClient\Exception\ExceptionInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; @@ -64,6 +65,7 @@ public function __construct( /** * @param array $caList * + * @throws UnprocessablePEM * @throws CertificateIsRevoked */ public function validateStatus(string $certificate, array $caList = []): void diff --git a/src/Violation/UnsupportedDomain.php b/src/Violation/UnsupportedDomain.php index a47ffba..42033e3 100644 --- a/src/Violation/UnsupportedDomain.php +++ b/src/Violation/UnsupportedDomain.php @@ -18,7 +18,7 @@ final class UnsupportedDomain extends Violation { private readonly string $requiredPattern; - /** @var array */ + /** @var array */ private readonly array $supported; public function __construct(string $requiredPattern, string ...$supported) diff --git a/src/X509DataExtractor.php b/src/X509DataExtractor.php index e29b490..959fdfd 100644 --- a/src/X509DataExtractor.php +++ b/src/X509DataExtractor.php @@ -21,6 +21,9 @@ final class X509DataExtractor private ?string $hash = null; private ?X509Info $fields = null; + /** + * @throws UnprocessablePEM + */ public function extractRawData(string $contents, string $name = '', bool $withPublicKey = false): X509Info { $hash = hash('sha256', $contents); diff --git a/src/X509Info.php b/src/X509Info.php index a6344f1..97723f0 100644 --- a/src/X509Info.php +++ b/src/X509Info.php @@ -25,7 +25,6 @@ final class X509Info /** @var array */ public readonly array $emails; public readonly string $fingerprint; - /** @var array */ public readonly string $pubKey; public readonly string $signatureAlgorithm; public readonly \DateTimeImmutable $validFrom; @@ -40,6 +39,9 @@ final class X509Info */ public readonly array $allFields; + /** + * @param array $fields + */ public function __construct(array $fields) { $this->altDomains = $fields['_alt_domains'] ?? []; diff --git a/tests/CertificateValidatorTest.php b/tests/CertificateValidatorTest.php index 99d4d46..c4df568 100644 --- a/tests/CertificateValidatorTest.php +++ b/tests/CertificateValidatorTest.php @@ -34,8 +34,8 @@ */ final class CertificateValidatorTest extends TestCase { - private ?CertificateValidator $certificateValidator = null; - private ?PdpManager $pdpManager; + private CertificateValidator $certificateValidator; + private PdpManager $pdpManager; protected function setUp(): void { @@ -70,7 +70,7 @@ public function validate_certificate_is_actually_readable(): void self::fail('Exception was expected.'); } catch (UnprocessablePEM $e) { self::assertSame(['name' => ''], $e->getParameters()); - self::assertSame($certContents, $e->getPrevious()->getPrevious()->getMessage()); + self::assertSame($certContents, $e->getPrevious()?->getPrevious()?->getMessage()); } } @@ -292,7 +292,7 @@ public function validate_certificate_data_is_readable(): void self::fail('Exception was expected.'); } catch (UnprocessablePEM $e) { - self::assertSame($certContents, $e->getPrevious()->getPrevious()->getMessage()); + self::assertSame($certContents, $e->getPrevious()?->getPrevious()?->getMessage()); self::assertSame([ 'name' => '', ], $e->getParameters()); diff --git a/tests/OCSPValidatorTest.php b/tests/OCSPValidatorTest.php index 3676a22..d1e58a7 100644 --- a/tests/OCSPValidatorTest.php +++ b/tests/OCSPValidatorTest.php @@ -21,13 +21,10 @@ use Prophecy\PhpUnit\ProphecyTrait; use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerInterface; -use Rollerworks\Component\PdbSfBridge\PdpMockProvider; use Rollerworks\Component\X509Validator\OCSPValidator; use Rollerworks\Component\X509Validator\TranslatableArgument; use Rollerworks\Component\X509Validator\Violation\CertificateIsRevoked; use Rollerworks\Component\X509Validator\Violation\UnprocessablePEM; -use Symfony\Component\Cache\Adapter\ArrayAdapter; -use Symfony\Component\Cache\Psr16Cache; use Symfony\Component\ErrorHandler\BufferingLogger; use Symfony\Component\HttpClient\HttpClient; use Symfony\Component\HttpClient\MockHttpClient; @@ -41,18 +38,7 @@ final class OCSPValidatorTest extends TestCase { use ProphecyTrait; - private ?OCSPValidator $certificateValidator = null; - private static ?Psr16Cache $cache; - - public static function setUpBeforeClass(): void - { - self::$cache = new Psr16Cache(new ArrayAdapter()); - } - - public static function tearDownAfterClass(): void - { - self::$cache = null; - } + private OCSPValidator $certificateValidator; protected function setUp(): void { @@ -60,8 +46,6 @@ protected function setUp(): void $httpClient = HttpClient::create(); $httpClient->setLogger(new BufferingLogger()); - $this->pdpManager = PdpMockProvider::getPdpManager(); - $this->certificateValidator = new OCSPValidator( httpClient: $httpClient, logger: $this->expectNoFailureLogs(), @@ -84,7 +68,7 @@ public function validate_certificate_is_actually_readable(): void self::fail('Exception was expected.'); } catch (UnprocessablePEM $e) { self::assertSame(['name' => ''], $e->getParameters()); - self::assertSame($certContents, $e->getPrevious()->getPrevious()->getMessage()); + self::assertSame($certContents, $e->getPrevious()?->getPrevious()?->getMessage()); } } @@ -103,7 +87,7 @@ public function validate_certificate_data_is_readable(): void self::fail('Exception was expected.'); } catch (UnprocessablePEM $e) { - self::assertSame($certContents, $e->getPrevious()->getPrevious()->getMessage()); + self::assertSame($certContents, $e->getPrevious()?->getPrevious()?->getMessage()); self::assertSame([ 'name' => '', ], $e->getParameters());