Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update coding standard #191

Merged
merged 1 commit into from
Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
name: "Continuous Integration"

env:
PHP_EXTENSIONS: "json, opcache"
PHP_EXTENSIONS: "json, mbstring, opcache"
PHP_INI_VALUES: "opcache.enable=1, opcache.enable_cli=1, zend.assertions=1, assert.exception=On"

jobs:
Expand Down Expand Up @@ -396,4 +396,4 @@ jobs:
flags: "phpunit,php-${{ matrix.php-version }},${{ matrix.coverage-drivers }}"

- name: "Run mutation tests with infection/infection"
run: "vendor/bin/infection --min-covered-msi=76 --min-msi=76 --coverage=.build/coverage"
run: "vendor/bin/infection --min-covered-msi=75 --min-msi=75 --coverage=.build/coverage"
10 changes: 5 additions & 5 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

declare(strict_types = 1);
$header = <<<'EOF'
This file is part of the ua-generic-request package.
This file is part of the ua-generic-request package.

Copyright (c) 2015-2021, Thomas Mueller <mimmi20@live.de>
Copyright (c) 2015-2021, Thomas Mueller <mimmi20@live.de>

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
EOF;
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
EOF;

$finder = PhpCsFixer\Finder::create()
->files()
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The MIT License (MIT)

Copyright (c) 2015-2020 Thomas Müller
Copyright (c) 2015-2021 Thomas Müller

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the _Software_), to deal in the Software without restriction, including without limitation the
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@
],
"require": {
"php": "^7.4.0 || ^8.0.0",
"ext-mbstring": "*",
"laminas/laminas-diactoros": "^2.5.0",
"mimmi20/browser-detector-loader-interface": "^4.0.0",
"mimmi20/browser-detector-loader-interface": "^4.0.1",
"psr/http-message": "^1.0.1"
},
"require-dev": {
"infection/infection": "^0.21.4",
"mimmi20/coding-standard": "^2.0.1",
"mimmi20/json-class": "^3.0.0",
"mimmi20/coding-standard": "^2.1.0",
"mimmi20/json-class": "^3.0.1",
"pepakriz/phpstan-exception-rules": "^0.11.6",
"phpstan/extension-installer": "^1.1.0",
"phpstan/phpstan": "^0.12.82",
"phpstan/phpstan-deprecation-rules": "^0.12.6",
"phpstan/phpstan-phpunit": "^0.12.18",
"phpunit/phpunit": "^9.5.3",
"phpunit/phpunit": "^9.5.4",
"symfony/finder": "^5.2.4"
},
"config": {
Expand Down
1 change: 1 addition & 0 deletions src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

declare(strict_types = 1);

namespace UaRequest;

/**
Expand Down
54 changes: 23 additions & 31 deletions src/GenericRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,21 @@
*/

declare(strict_types = 1);

namespace UaRequest;

use BrowserDetector\Loader\NotFoundException;
use Psr\Http\Message\MessageInterface;
use UaRequest\Header\HeaderInterface;
use UaRequest\Header\HeaderLoaderInterface;

use function array_filter;
use function array_key_exists;
use function array_keys;
use function is_string;

final class GenericRequest implements GenericRequestInterface
{
/** @var array */
private $headers = [];

/** @var HeaderInterface[] */
private $filteredHeaders = [];

/** @var HeaderLoaderInterface */
private $loader;

private const HEADERS = [
Constants::HEADER_DEVICE_UA,
Constants::HEADER_UCBROWSER_UA,
Expand All @@ -51,32 +48,39 @@ final class GenericRequest implements GenericRequestInterface
Constants::HEADER_WAP_PROFILE,
Constants::HEADER_NB_CONTENT,
];
/** @var array<string, string> */
private array $headers = [];

/** @var array<HeaderInterface> */
private array $filteredHeaders = [];

private HeaderLoaderInterface $loader;

/**
* @param MessageInterface $message
* @param HeaderLoaderInterface $loader
*/
public function __construct(MessageInterface $message, HeaderLoaderInterface $loader)
{
$this->loader = $loader;

foreach (array_keys($message->getHeaders()) as $header) {
if (!is_string($header)) {
continue;
}

$this->headers[$header] = $message->getHeaderLine($header);
}

$this->filterHeaders();
}

/**
* @return array
* @return array<string, string>
*/
public function getHeaders(): array
{
return $this->headers;
}

/**
* @return array
* @return array<string>
*/
public function getFilteredHeaders(): array
{
Expand All @@ -89,9 +93,6 @@ public function getFilteredHeaders(): array
return $headers;
}

/**
* @return string
*/
public function getBrowserUserAgent(): string
{
foreach ($this->filteredHeaders as $header) {
Expand All @@ -103,9 +104,6 @@ public function getBrowserUserAgent(): string
return '';
}

/**
* @return string
*/
public function getDeviceUserAgent(): string
{
foreach ($this->filteredHeaders as $header) {
Expand All @@ -117,9 +115,6 @@ public function getDeviceUserAgent(): string
return '';
}

/**
* @return string
*/
public function getPlatformUserAgent(): string
{
foreach ($this->filteredHeaders as $header) {
Expand All @@ -131,9 +126,6 @@ public function getPlatformUserAgent(): string
return '';
}

/**
* @return string
*/
public function getEngineUserAgent(): string
{
foreach ($this->filteredHeaders as $header) {
Expand All @@ -145,13 +137,13 @@ public function getEngineUserAgent(): string
return '';
}

/**
* @return void
*/
private function filterHeaders(): void
{
$headers = $this->headers;
$filtered = array_filter(self::HEADERS, static fn ($value): bool => array_key_exists($value, $headers));
$filtered = array_filter(
self::HEADERS,
static fn ($value): bool => array_key_exists($value, $headers)
);

foreach ($filtered as $header) {
try {
Expand Down
23 changes: 7 additions & 16 deletions src/GenericRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@
*/

declare(strict_types = 1);

namespace UaRequest;

use Laminas\Diactoros\HeaderSecurity;
use Laminas\Diactoros\ServerRequestFactory;
use Psr\Http\Message\MessageInterface;
use UaRequest\Header\HeaderLoader;

use function mb_strpos;
use function mb_strtoupper;
use function preg_replace;
use function str_replace;

final class GenericRequestFactory implements GenericRequestFactoryInterface
{
/**
* Creates Generic Request from the given HTTP Request (normally $_SERVER).
*
* @param array $headers HTTP Request
*
* @return \UaRequest\GenericRequest
* @param array<string, string> $headers HTTP Request
*/
public function createRequestFromArray(array $headers): GenericRequest
{
Expand All @@ -50,10 +54,6 @@ public function createRequestFromArray(array $headers): GenericRequest

/**
* Create a Generic Request from the given $userAgent
*
* @param string $userAgent
*
* @return \UaRequest\GenericRequest
*/
public function createRequestFromString(string $userAgent): GenericRequest
{
Expand All @@ -68,21 +68,12 @@ public function createRequestFromString(string $userAgent): GenericRequest

/**
* Create a Generic Request from a given PSR-7 HTTP message
*
* @param \Psr\Http\Message\MessageInterface $message
*
* @return \UaRequest\GenericRequest
*/
public function createRequestFromPsr7Message(MessageInterface $message): GenericRequest
{
return new GenericRequest($message, new HeaderLoader());
}

/**
* @param string $header
*
* @return string
*/
private function filterHeader(string $header): string
{
return (string) preg_replace(
Expand Down
13 changes: 2 additions & 11 deletions src/GenericRequestFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

declare(strict_types = 1);

namespace UaRequest;

use Psr\Http\Message\MessageInterface;
Expand All @@ -18,27 +19,17 @@ interface GenericRequestFactoryInterface
/**
* Creates Generic Request from the given HTTP Request (normally $_SERVER).
*
* @param array $headers HTTP Request
*
* @return \UaRequest\GenericRequest
* @param array<string, string> $headers HTTP Request
*/
public function createRequestFromArray(array $headers): GenericRequest;

/**
* Create a Generic Request from the given $userAgent
*
* @param string $userAgent
*
* @return \UaRequest\GenericRequest
*/
public function createRequestFromString(string $userAgent): GenericRequest;

/**
* Create a Generic Request from a given PSR-7 HTTP message
*
* @param \Psr\Http\Message\MessageInterface $message
*
* @return \UaRequest\GenericRequest
*/
public function createRequestFromPsr7Message(MessageInterface $message): GenericRequest;
}
17 changes: 3 additions & 14 deletions src/GenericRequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,26 @@
*/

declare(strict_types = 1);

namespace UaRequest;

interface GenericRequestInterface
{
/**
* @return array
* @return array<string, string>
*/
public function getHeaders(): array;

/**
* @return array
* @return array<string>
*/
public function getFilteredHeaders(): array;

/**
* @return string
*/
public function getBrowserUserAgent(): string;

/**
* @return string
*/
public function getDeviceUserAgent(): string;

/**
* @return string
*/
public function getPlatformUserAgent(): string;

/**
* @return string
*/
public function getEngineUserAgent(): string;
}
Loading