diff --git a/.travis.yml b/.travis.yml index 288b18f..ed91078 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,3 +16,4 @@ install: script: - vendor/bin/phpunit - vendor/bin/psalm + - vendor/bin/phpcs diff --git a/composer.json b/composer.json index 20b61c1..bcf72e8 100644 --- a/composer.json +++ b/composer.json @@ -36,6 +36,7 @@ "require-dev": { "psr/http-message": "^1", "phpunit/phpunit": "4.*|5.*", + "squizlabs/php_codesniffer": "^3", "vimeo/psalm": "^0.3" }, "suggest": { diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..c9972e6 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,10 @@ + + + + + + + bin + src + test + diff --git a/src/CSPBuilder.php b/src/CSPBuilder.php index a92bbc4..a9ce45c 100644 --- a/src/CSPBuilder.php +++ b/src/CSPBuilder.php @@ -89,7 +89,7 @@ public function compile(): string $compiled = []; - foreach(self::$directives as $dir) { + foreach (self::$directives as $dir) { if (\in_array($dir, $ruleKeys)) { if (empty($ruleKeys)) { if ($dir === 'base-uri') { @@ -278,7 +278,7 @@ public static function fromData($data = ''): self { $array = \json_decode($data, true); - if(!\is_array($array)) { + if (!\is_array($array)) { throw new \Exception('Is not array valid'); } @@ -369,7 +369,7 @@ public function preHash( * @param bool $legacy * @return \Psr\Http\Message\MessageInterface */ - function injectCSPHeader(MessageInterface $message, bool $legacy = false): MessageInterface + public function injectCSPHeader(MessageInterface $message, bool $legacy = false): MessageInterface { if ($this->needsCompile) { $this->compile(); @@ -596,7 +596,8 @@ protected function compileSubgroup(string $directive, $policies = []): string if ($url !== false) { if ($this->supportOldBrowsers) { if (\strpos($url, '://') === false) { - if (($this->isHTTPSConnection() && $this->httpsTransformOnHttpsConnections) || !empty($this->policies['upgrade-insecure-requests'])) { + if (($this->isHTTPSConnection() && $this->httpsTransformOnHttpsConnections) + || !empty($this->policies['upgrade-insecure-requests'])) { // We only want HTTPS connections here. $ret .= 'https://'.$url.' '; } else { @@ -604,7 +605,8 @@ protected function compileSubgroup(string $directive, $policies = []): string } } } - if (($this->isHTTPSConnection() && $this->httpsTransformOnHttpsConnections) || !empty($this->policies['upgrade-insecure-requests'])) { + if (($this->isHTTPSConnection() && $this->httpsTransformOnHttpsConnections) + || !empty($this->policies['upgrade-insecure-requests'])) { $ret .= \str_replace('http://', 'https://', $url).' '; } else { $ret .= $url.' '; diff --git a/test/BasicTest.php b/test/BasicTest.php index 1ea4851..82d0ba4 100644 --- a/test/BasicTest.php +++ b/test/BasicTest.php @@ -1,9 +1,10 @@ getCompiledHeader() ); - + $noOld = file_get_contents(__DIR__.'/vectors/basic-csp-no-old.out'); // We expect different output for ytimg.com when we disable legacy // browser support (i.e. Safari): @@ -24,7 +25,7 @@ public function testBasicFromFile() ->disableOldBrowserSupport() ->getCompiledHeader() ); - + $array = $basic->getHeaderArray(); $this->assertEquals( $array, @@ -34,8 +35,8 @@ public function testBasicFromFile() 'X-Webkit-CSP' => $noOld ] ); - - + + $array2 = $basic->getHeaderArray(false); $this->assertEquals( $array2, @@ -48,7 +49,7 @@ public function testBasicFromFile() public function testBasicFromData() { $data = file_get_contents(__DIR__.'/vectors/basic-csp.json'); - + $basic = CSPBuilder::fromData($data); $basic->addSource('img-src', 'ytimg.com'); @@ -57,7 +58,7 @@ public function testBasicFromData() $basic->getCompiledHeader() ); } - + public function testHash() { $basic = CSPBuilder::fromFile(__DIR__.'/vectors/basic-csp.json'); @@ -67,7 +68,7 @@ public function testHash() $basic->getCompiledHeader() ); } - + public function testPreHash() { $basic = CSPBuilder::fromFile(__DIR__.'/vectors/basic-csp.json'); @@ -87,7 +88,8 @@ public function testPreHash() public function testSourceHttpsConversion() { /** @var CSPBuilder|\PHPUnit_Framework_MockObject_MockObject $cspHttp */ - $cspHttp = $this->getMockBuilder(CSPBuilder::class)->setMethods(['isHTTPSConnection'])->disableOriginalConstructor()->getMock(); + $cspHttp = $this->getMockBuilder(CSPBuilder::class)->setMethods(['isHTTPSConnection']) + ->disableOriginalConstructor()->getMock(); $cspHttp->method('isHTTPSConnection')->willReturn(false); $cspHttp->addSource('form', 'http://example.com'); @@ -98,7 +100,8 @@ public function testSourceHttpsConversion() $this->assertContains('http://another.com', $compiledCspHttp); /** @var CSPBuilder|\PHPUnit_Framework_MockObject_MockObject $cspHttps */ - $cspHttps = $this->getMockBuilder(CSPBuilder::class)->setMethods(['isHTTPSConnection'])->disableOriginalConstructor()->getMock(); + $cspHttps = $this->getMockBuilder(CSPBuilder::class)->setMethods(['isHTTPSConnection']) + ->disableOriginalConstructor()->getMock(); $cspHttps->method('isHTTPSConnection')->willReturn(true); $cspHttps->addSource('form', 'http://example.com');