diff --git a/.gitattributes b/.gitattributes index 66ad8a0..28199f2 100644 --- a/.gitattributes +++ b/.gitattributes @@ -7,3 +7,4 @@ /.scrutinizer.yml export-ignore /.travis.yml export-ignore /phpunit.xml export-ignore +/.php_cs.dist export-ignore diff --git a/.php_cs.dist b/.php_cs.dist new file mode 100644 index 0000000..f7d7a35 --- /dev/null +++ b/.php_cs.dist @@ -0,0 +1,18 @@ +setUsingCache(false) + ->setRules([ + 'native_function_invocation' => [ + 'exclude' => [] + ], + ]) + ->setRiskyAllowed(true) + ->setFinder( + PhpCsFixer\Finder::create() + ->in(__DIR__ . '/src') + ->append([__FILE__]) + ) +; diff --git a/.travis.yml b/.travis.yml index 258b24d..3153955 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,11 @@ matrix: - php vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover after_script: - php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover + - php: 7.3 + script: + - composer test-cs + - composer test-md + - composer test-cs-fixer before_script: - travis_retry composer self-update - travis_retry composer install --no-interaction --prefer-dist diff --git a/composer.json b/composer.json index 4b1eec5..dee7b7c 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,8 @@ "mockery/mockery": "^1.0", "scrutinizer/ocular": "^1.4", "squizlabs/php_codesniffer": "^2.9", - "phpmd/phpmd": "^2.6" + "phpmd/phpmd": "^2.6", + "friendsofphp/php-cs-fixer": "^2.14" }, "minimum-stability": "stable", "autoload": { @@ -46,10 +47,12 @@ } }, "scripts": { - "test": ["@test-unit", "@test-cs", "@test-md"], + "test": ["@test-unit", "@test-cs", "@test-md", "@test-cs-fixer"], "test-unit": "./vendor/phpunit/phpunit/phpunit --coverage-text", "test-unit-phpdbg": "phpdbg -qrr ./vendor/bin/phpunit --coverage-text", "test-cs": "./vendor/bin/phpcs -p -s --standard=PSR2 ./src ./tests", - "test-md": "./vendor/bin/phpmd ./src text codesize,controversial,cleancode,design,unusedcode,naming" + "test-md": "./vendor/bin/phpmd ./src text codesize,controversial,cleancode,design,unusedcode,naming", + "test-cs-fixer": "./vendor/bin/php-cs-fixer fix --diff --dry-run -v", + "cs-fixer": "./vendor/bin/php-cs-fixer fix --diff -v --ansi" } } diff --git a/src/Analyzer.php b/src/Analyzer.php index 04e2c45..73a303b 100644 --- a/src/Analyzer.php +++ b/src/Analyzer.php @@ -213,7 +213,7 @@ protected function analyzeAsPreFlight(RequestInterface $request, string $request return $this->createResult(AnalysisResultInterface::TYPE_REQUEST_OUT_OF_CORS_SCOPE); } - $requestMethod = reset($requestMethod); + $requestMethod = \reset($requestMethod); // OK now we are sure it's a pre-flight request $this->logDebug('Request is identified as a pre-flight CORS request.'); @@ -280,7 +280,7 @@ protected function createPreFlightResponseHeaders( // #6.2.10 // Has only 'simple' headers excluding Content-Type - $isSimpleExclCT = empty(array_diff($lcRequestHeaders, static::SIMPLE_LC_HEADERS_EXCLUDING_CONTENT_TYPE)); + $isSimpleExclCT = empty(\array_diff($lcRequestHeaders, static::SIMPLE_LC_HEADERS_EXCLUDING_CONTENT_TYPE)); if ($isSimpleExclCT === false || $this->strategy->isForceAddAllowedHeadersToPreFlightResponse() === true) { $headers[CorsResponseHeaders::ALLOW_HEADERS] = $this->strategy->getRequestAllowedHeaders($request); } @@ -298,10 +298,10 @@ protected function getRequestedHeadersInLowerCase(RequestInterface $request): ar $requestHeaders = []; foreach ($request->getHeader(CorsRequestHeaders::HEADERS) as $headersList) { - $headersList = strtolower($headersList); - foreach (explode(CorsRequestHeaders::HEADERS_SEPARATOR, $headersList) as $header) { + $headersList = \strtolower($headersList); + foreach (\explode(CorsRequestHeaders::HEADERS_SEPARATOR, $headersList) as $header) { // after explode header names might have spaces in the beginnings and ends so trim them - $header = trim($header); + $header = \trim($header); if (empty($header) === false) { $requestHeaders[] = $header; } @@ -321,7 +321,7 @@ protected function getOriginHeader(RequestInterface $request): string if ($request->hasHeader(CorsRequestHeaders::ORIGIN) === true) { $header = $request->getHeader(CorsRequestHeaders::ORIGIN); if (empty($header) === false) { - return reset($header); + return \reset($header); } } @@ -348,15 +348,15 @@ protected function checkIsSameHost(RequestInterface $request): bool // // `parse_url` function thinks the first value is `path` and the second is `host` with `port` // which is a bit annoying so... - $portOrNull = parse_url($host, PHP_URL_PORT); - $hostUrl = $portOrNull === null ? $host : parse_url($host, PHP_URL_HOST); + $portOrNull = \parse_url($host, PHP_URL_PORT); + $hostUrl = $portOrNull === null ? $host : \parse_url($host, PHP_URL_HOST); // Neither MDN, nor RFC tell anything definitive about Host header comparison. // Browsers such as Firefox and Chrome do not add the optional port for // HTTP (80) and HTTPS (443). // So we require port match only if it specified in settings. - $isHostUrlMatch = strcasecmp($serverOriginHost, $hostUrl) === 0; + $isHostUrlMatch = \strcasecmp($serverOriginHost, $hostUrl) === 0; $isSameHost = $isHostUrlMatch === true && ($serverOriginPort === null || $serverOriginPort === $portOrNull); @@ -381,7 +381,7 @@ protected function checkIsSameHost(RequestInterface $request): bool */ protected function checkIsCrossOrigin(string $requestOrigin): bool { - $parsedUrl = parse_url($requestOrigin); + $parsedUrl = \parse_url($requestOrigin); if ($parsedUrl === false) { $this->logWarning('Request origin header URL cannot be parsed.', ['url' => $requestOrigin]); @@ -391,12 +391,12 @@ protected function checkIsCrossOrigin(string $requestOrigin): bool // check `host` parts $requestOriginHost = $parsedUrl['host'] ?? ''; $serverOriginHost = $this->strategy->getServerOriginHost(); - if (strcasecmp($requestOriginHost, $serverOriginHost) !== 0) { + if (\strcasecmp($requestOriginHost, $serverOriginHost) !== 0) { return true; } // check `port` parts - $requestOriginPort = array_key_exists('port', $parsedUrl) === true ? (int)$parsedUrl['port'] : null; + $requestOriginPort = \array_key_exists('port', $parsedUrl) === true ? (int)$parsedUrl['port'] : null; $serverOriginPort = $this->strategy->getServerOriginPort(); if ($requestOriginPort !== $serverOriginPort) { return true; @@ -405,7 +405,7 @@ protected function checkIsCrossOrigin(string $requestOrigin): bool // check `scheme` parts $requestOriginScheme = $parsedUrl['scheme'] ?? ''; $serverOriginScheme = $this->strategy->getServerOriginScheme(); - if (strcasecmp($requestOriginScheme, $serverOriginScheme) !== 0) { + if (\strcasecmp($requestOriginScheme, $serverOriginScheme) !== 0) { return true; } @@ -450,7 +450,7 @@ protected static function getFactory(): FactoryInterface private function getRequestHostHeader(RequestInterface $request): ?string { $hostHeaderValue = $request->getHeader(CorsRequestHeaders::HOST); - $host = empty($hostHeaderValue) === true ? null : reset($hostHeaderValue); + $host = empty($hostHeaderValue) === true ? null : \reset($hostHeaderValue); return $host; } diff --git a/src/Strategies/Settings.php b/src/Strategies/Settings.php index 935cd84..8d1dcb6 100644 --- a/src/Strategies/Settings.php +++ b/src/Strategies/Settings.php @@ -263,16 +263,16 @@ public function getServerOriginPort(): ?int */ public function setServerOrigin(string $scheme, string $host, int $port): self { - assert(empty($scheme) === false); - assert(empty($host) === false); - assert(0 < $port && $port <= 0xFFFF); + \assert(empty($scheme) === false); + \assert(empty($host) === false); + \assert(0 < $port && $port <= 0xFFFF); $this->serverOriginScheme = $scheme; $this->serverOriginHost = $host; - if (strcasecmp($scheme, 'http') === 0 && $port === 80) { + if (\strcasecmp($scheme, 'http') === 0 && $port === 80) { $port = null; - } elseif (strcasecmp($scheme, 'https') === 0 && $port === 443) { + } elseif (\strcasecmp($scheme, 'https') === 0 && $port === 443) { $port = null; } $this->serverOriginPort = $port; @@ -305,7 +305,7 @@ public function getPreFlightCacheMaxAge(RequestInterface $request): int */ public function setPreFlightCacheMaxAge(int $cacheMaxAge): self { - assert($cacheMaxAge >= 0); + \assert($cacheMaxAge >= 0); $this->preFlightCacheMaxAge = $cacheMaxAge; $this->isPreFlightCanBeCached = $cacheMaxAge > 0; @@ -428,7 +428,7 @@ public function isRequestOriginAllowed(string $requestOrigin): bool { return $this->areAllOriginsAllowed === true || - isset($this->allowedOrigins[strtolower($requestOrigin)]) === true; + isset($this->allowedOrigins[\strtolower($requestOrigin)]) === true; } /** @@ -455,7 +455,7 @@ public function setAllowedOrigins(array $origins): self $this->allowedOrigins = []; foreach ($origins as $origin) { - $this->allowedOrigins[strtolower($origin)] = true; + $this->allowedOrigins[\strtolower($origin)] = true; } $this->areAllOriginsAllowed = false; @@ -468,7 +468,7 @@ public function setAllowedOrigins(array $origins): self */ public function isRequestMethodSupported(string $method): bool { - return $this->areAllMethodsAllowed === true || isset($this->allowedLcMethods[strtolower($method)]) === true; + return $this->areAllMethodsAllowed === true || isset($this->allowedLcMethods[\strtolower($method)]) === true; } /** @@ -500,11 +500,11 @@ public function enableAllMethodsAllowed(): self */ public function setAllowedMethods(array $methods): self { - $this->allowedMethodsList = implode(', ', $methods); + $this->allowedMethodsList = \implode(', ', $methods); $this->allowedLcMethods = []; foreach ($methods as $method) { - $this->allowedLcMethods[strtolower($method)] = true; + $this->allowedLcMethods[\strtolower($method)] = true; } $this->areAllMethodsAllowed = false; @@ -518,7 +518,7 @@ public function setAllowedMethods(array $methods): self public function isRequestAllHeadersSupported(array $lcHeaders): bool { return $this->areAllHeadersAllowed === true || - count(array_intersect($this->allowedLcHeaders, $lcHeaders)) === count($lcHeaders); + \count(\array_intersect($this->allowedLcHeaders, $lcHeaders)) === \count($lcHeaders); } /** @@ -550,11 +550,11 @@ public function enableAllHeadersAllowed(): self */ public function setAllowedHeaders(array $headers): self { - $this->allowedHeadersList = implode(', ', $headers); + $this->allowedHeadersList = \implode(', ', $headers); $this->allowedLcHeaders = []; foreach ($headers as $header) { - $this->allowedLcHeaders[] = strtolower($header); + $this->allowedLcHeaders[] = \strtolower($header); } $this->areAllHeadersAllowed = false; @@ -599,12 +599,12 @@ public function setExposedHeaders(array $headers): self // make sense to include those headers to exposed. $filtered = []; foreach ($headers as $header) { - if (in_array(strtolower($header), static::SIMPLE_LC_RESPONSE_HEADERS) === false) { + if (\in_array(\strtolower($header), static::SIMPLE_LC_RESPONSE_HEADERS) === false) { $filtered[] = $header; } } - $this->exposedHeadersList = implode(', ', $filtered); + $this->exposedHeadersList = \implode(', ', $filtered); return $this; }