diff --git a/src/CSPBuilder.php b/src/CSPBuilder.php index 9b360f0..24f26b3 100644 --- a/src/CSPBuilder.php +++ b/src/CSPBuilder.php @@ -159,7 +159,10 @@ public function compile(): string if (!is_string($this->policies['report-uri'])) { throw new TypeError('report-uri policy somehow not a string'); } - $compiled [] = sprintf('report-uri %s; ', $this->enc($this->policies['report-uri']), 'report-uri'); + $compiled []= sprintf( + 'report-uri %s; ', + $this->enc($this->policies['report-uri'], 'report-uri') + ); } if (!empty($this->policies['report-to'])) { if (!is_string($this->policies['report-to'])) { @@ -178,7 +181,10 @@ public function compile(): string return $this->compiled; } - public function compileReportEndpoints() + /** + * @psalm-suppress TypeDoesNotContainType + */ + public function compileReportEndpoints(): string { if (!empty($this->reportEndpoints) && $this->needsCompileEndpoints) { // If it's a string, it's probably something like `report-to: key=endpoint @@ -186,22 +192,21 @@ public function compileReportEndpoints() if (!is_array($this->reportEndpoints)) { throw new TypeError('Report endpoints is not an array'); } - if (is_array($this->reportEndpoints)) { - $jsonValidator = new Validator(); - $reportTo = []; - $schema = file_get_contents(__DIR__ . '/../schema/reportto.json'); - foreach ($this->reportEndpoints as $reportEndpoint) { - $reportEndpointAsJSON = \Opis\JsonSchema\Helper::toJSON($reportEndpoint); - $isValid = $jsonValidator->validate($reportEndpointAsJSON, $schema); - if ($isValid->isValid()) { - $reportTo[] = json_encode($reportEndpointAsJSON); - } + $jsonValidator = new Validator(); + $reportTo = []; + $schema = file_get_contents(__DIR__ . '/../schema/reportto.json'); + foreach ($this->reportEndpoints as $reportEndpoint) { + $reportEndpointAsJSON = \Opis\JsonSchema\Helper::toJSON($reportEndpoint); + $isValid = $jsonValidator->validate($reportEndpointAsJSON, $schema); + if ($isValid->isValid()) { + $reportTo[] = json_encode($reportEndpointAsJSON); } - $this->compiledEndpoints = rtrim(implode(',', $reportTo)); } + $this->compiledEndpoints = rtrim(implode(',', $reportTo)); $this->needsCompileEndpoints = false; } + return $this->compiledEndpoints; } /** @@ -317,7 +322,7 @@ public function addDirective(string $key, $value = null): self * @param array|string $reportEndpoint * @return void */ - public function addReportEndpoints(array|string $reportEndpoint): void + public function addReportEndpoints($reportEndpoint): void { $this->needsCompileEndpoints = true; $this->reportEndpoints[] = Helper::toJSON($reportEndpoint); @@ -418,6 +423,8 @@ public static function fromFile(string $filename = ''): self * @param string $header * @return self * @throws Exception + * + * @psalm-suppress DocblockTypeContradiction */ public static function fromHeader(string $header = ''): self { @@ -428,7 +435,7 @@ public static function fromHeader(string $header = ''): self foreach ($directives as $directive) { [$name, $values] = explode(' ', trim($directive), 2) + [null, null]; - if (null === $name) { + if (is_null($name)) { continue; } @@ -511,6 +518,7 @@ public function getCompiledReportEndpointsHeader(): string */ public function getHeaderArray(bool $legacy = true): array { + $return = []; if ($this->needsCompile) { $this->compile(); } @@ -544,7 +552,7 @@ public function getRequireHeaders(): array } /** - * @return array|string + * @return array */ public function getReportEndpoints(): array { @@ -602,7 +610,7 @@ public function injectCSPHeader(MessageInterface $message, bool $legacy = false) $message = $message->withAddedHeader($key, $this->compiled); } if (!empty($this->compileReportEndpoints())) { - $message = $message->withAddedHeader('report-to', $this->reportTo); + $message = $message->withAddedHeader('report-to', $this->compiledEndpoints); } return $message; @@ -874,7 +882,7 @@ public function removeDirective(string $key): self * @param array|string $reportEndpoints * @return void */ - public function setReportEndpoints(array|string $reportEndpoints): void + public function setReportEndpoints($reportEndpoints): void { $this->needsCompileEndpoints = true; $toJSON = Helper::toJSON($reportEndpoints); @@ -883,8 +891,11 @@ public function setReportEndpoints(array|string $reportEndpoints): void $this->reportEndpoints = $toJSON; } - - public function removeReportEndpoint(string $key) + /** + * @param string $key + * @return void + */ + public function removeReportEndpoint(string $key): void { foreach ($this->reportEndpoints as $idx => $endpoint) { if ($endpoint->group === $key) {