From 4ffb0cdfa9914aefdddf9a925ca7a9f5fdb40e0d Mon Sep 17 00:00:00 2001 From: Mohan Raj Date: Thu, 5 Oct 2023 12:25:13 +0530 Subject: [PATCH 1/4] Fix PHPStan failures Errors: PHPStan: src/Configuration.php#L120 Dead catch - Dflydev\DotAccessData\Exception\MissingPathException is never thrown in the try block. PHPStan: src/Configuration.php#L193 Parameter 1 $data of method Dflydev\DotAccessData\Data::import() expects array, array given. --- src/Configuration.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Configuration.php b/src/Configuration.php index 6294367..a922446 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -117,8 +117,12 @@ public function get(string $key) $this->build(self::getTopLevelKey($key)); return $this->cache[$key] = $this->finalConfig->get($key); - } catch (InvalidPathException | MissingPathException $ex) { - throw new UnknownOptionException($ex->getMessage(), $key, (int) $ex->getCode(), $ex); + } catch (\Exception $ex) { + if ($ex instanceof InvalidPathException || $ex instanceof MissingPathException) { + throw new UnknownOptionException($ex->getMessage(), $key, (int) $ex->getCode(), $ex); + } + + throw $ex; } } @@ -186,6 +190,7 @@ private function build(string $topLevelKey): void $schema = $this->configSchemas[$topLevelKey]; $processor = new Processor(); + /** @var \StdClass $processed */ $processed = $processor->process(Expect::structure([$topLevelKey => $schema]), $userData); $this->raiseAnyDeprecationNotices($processor->getWarnings()); From ea0efb5ac94bfaf3c4583866432c2718146b6e87 Mon Sep 17 00:00:00 2001 From: Colin O'Dell Date: Thu, 5 Oct 2023 10:50:11 -0400 Subject: [PATCH 2/4] Only handle known exceptions --- src/Configuration.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/Configuration.php b/src/Configuration.php index a922446..8de0873 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -17,7 +17,6 @@ use Dflydev\DotAccessData\DataInterface; use Dflydev\DotAccessData\Exception\DataException; use Dflydev\DotAccessData\Exception\InvalidPathException; -use Dflydev\DotAccessData\Exception\MissingPathException; use League\Config\Exception\UnknownOptionException; use League\Config\Exception\ValidationException; use Nette\Schema\Expect; @@ -117,12 +116,8 @@ public function get(string $key) $this->build(self::getTopLevelKey($key)); return $this->cache[$key] = $this->finalConfig->get($key); - } catch (\Exception $ex) { - if ($ex instanceof InvalidPathException || $ex instanceof MissingPathException) { - throw new UnknownOptionException($ex->getMessage(), $key, (int) $ex->getCode(), $ex); - } - - throw $ex; + } catch (InvalidPathException $ex) { + throw new UnknownOptionException($ex->getMessage(), $key, (int) $ex->getCode(), $ex); } } From 25fc7bb41d67a2c5b1c83f0ada79ae76ee4e497c Mon Sep 17 00:00:00 2001 From: Colin O'Dell Date: Thu, 5 Oct 2023 10:50:22 -0400 Subject: [PATCH 3/4] Use assert() instead --- src/Configuration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Configuration.php b/src/Configuration.php index 8de0873..1d5c1b5 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -185,8 +185,8 @@ private function build(string $topLevelKey): void $schema = $this->configSchemas[$topLevelKey]; $processor = new Processor(); - /** @var \StdClass $processed */ $processed = $processor->process(Expect::structure([$topLevelKey => $schema]), $userData); + \assert($processed instanceof \stdClass); $this->raiseAnyDeprecationNotices($processor->getWarnings()); From a0821f6a0fb8454147e58f0c32440ce06f50ef05 Mon Sep 17 00:00:00 2001 From: Colin O'Dell Date: Thu, 5 Oct 2023 10:50:32 -0400 Subject: [PATCH 4/4] Remove redundant cast --- src/Configuration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Configuration.php b/src/Configuration.php index 1d5c1b5..a1bb57d 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -190,7 +190,7 @@ private function build(string $topLevelKey): void $this->raiseAnyDeprecationNotices($processor->getWarnings()); - $this->finalConfig->import((array) self::convertStdClassesToArrays($processed)); + $this->finalConfig->import(self::convertStdClassesToArrays($processed)); } catch (NetteValidationException $ex) { throw new ValidationException($ex); }