Skip to content

Commit

Permalink
fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mimmi20 committed Jan 15, 2025
1 parent 90196e0 commit 37b8649
Show file tree
Hide file tree
Showing 96 changed files with 842 additions and 752 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"mimmi20/macos-build": "^4.0.0",
"mimmi20/ua-browser-type": "^11.0.0",
"mimmi20/ua-device-type": "^11.0.0",
"mimmi20/ua-generic-request": "^10.0.0",
"mimmi20/ua-generic-request": "^10.0.1",
"mimmi20/ua-loader-interface": "^4.0.1",
"mimmi20/ua-normalizer": "^7.0.0",
"mimmi20/ua-parser-interface": "^2.0.0",
Expand Down
23 changes: 9 additions & 14 deletions src/Detector.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Psr\Http\Message\MessageInterface;
use Psr\Log\LoggerInterface;
use Psr\SimpleCache\InvalidArgumentException;
use UaBrowserType\Unknown;
use UaDeviceType\Type;
use UaLoader\BrowserLoaderInterface;
use UaLoader\Data\ClientDataInterface;
Expand All @@ -37,18 +36,18 @@
use UaRequest\Header\HeaderInterface;
use UaRequest\RequestBuilderInterface;
use UaResult\Browser\Browser;
use UaResult\Browser\BrowserInterface;
use UaResult\Company\Company;
use UaResult\Device\Device;
use UaResult\Device\DeviceInterface;
use UaResult\Device\Display;
use UaResult\Engine\Engine;
use UaResult\Engine\EngineInterface;
use UaResult\Os\Os;
use UaResult\Os\OsInterface;
use UnexpectedValueException;

use function array_change_key_case;
use function array_filter;
use function array_map;
use function assert;
use function explode;
use function is_array;
Expand All @@ -61,6 +60,8 @@
use function str_contains;
use function trim;

use const CASE_LOWER;

final readonly class Detector implements DetectorInterface
{
/**
Expand Down Expand Up @@ -115,7 +116,7 @@ public function getBrowser(array | GenericRequestInterface | MessageInterface |
}

/**
* @return array{headers: array<non-empty-string, non-empty-string>, device: array{architecture: string|null, deviceName: string|null, marketingName: string|null, manufacturer: string|null, brand: string|null, dualOrientation: bool|null, simCount: int|null, display: array{width: int|null, height: int|null, touch: bool|null, size: float|null}, type: string|null, ismobile: bool, istv: bool, bits: int|null}, os: array{name: string|null, marketingName: string|null, version: string|null, manufacturer: string|null}, client: array{name: string|null, version: string|null, manufacturer: string|null, type: string|null, isbot: bool}, engine: array{name: string|null, version: string|null, manufacturer: string|null}}
* @return array{headers: array<non-empty-string, string>, device: array{architecture: string|null, deviceName: string|null, marketingName: string|null, manufacturer: string|null, brand: string|null, dualOrientation: bool|null, simCount: int|null, display: array{width: int|null, height: int|null, touch: bool|null, size: float|null}, type: string|null, ismobile: bool, istv: bool, bits: int|null}, os: array{name: string|null, marketingName: string|null, version: string|null, manufacturer: string|null}, client: array{name: string|null, version: string|null, manufacturer: string|null, type: string|null, isbot: bool}, engine: array{name: string|null, version: string|null, manufacturer: string|null}}
*
* @throws UnexpectedValueException
*/
Expand All @@ -127,11 +128,9 @@ private function parse(GenericRequestInterface $request): array
/* detect device */
$deviceIsMobile = $this->getDeviceIsMobile(filteredHeaders: $filteredHeaders);

$deviceData = $this->getDeviceData(
filteredHeaders: $filteredHeaders,
);
$deviceData = $this->getDeviceData(filteredHeaders: $filteredHeaders);

$device = $deviceData->getDevice();
$device = $deviceData->getDevice();
$deviceMarketingName = $device->getMarketingName();

/* detect platform */
Expand Down Expand Up @@ -164,9 +163,7 @@ private function parse(GenericRequestInterface $request): array
}

/* detect client */
$clientData = $this->getClientData(
filteredHeaders: $filteredHeaders,
);
$clientData = $this->getClientData(filteredHeaders: $filteredHeaders);

$client = $clientData->getClient();

Expand All @@ -186,9 +183,7 @@ private function parse(GenericRequestInterface $request): array

return [
'headers' => array_map(
callback: function(HeaderInterface $header): string {
return $header->getValue();
},
callback: static fn (HeaderInterface $header): string => $header->getValue(),
array: array_change_key_case($request->getHeaders(), CASE_LOWER),

Check warning on line 187 in src/Detector.php

View workflow job for this annotation

GitHub Actions / UnitTests / Code Coverage with PHPUnit (ubuntu-24.04, 8.3, lowest)

Escaped Mutant for Mutator "UnwrapArrayChangeKeyCase": @@ @@ // }, // array: $request->getHeaders() // )); - return ['headers' => array_map(callback: static fn(HeaderInterface $header): string => $header->getValue(), array: array_change_key_case($request->getHeaders(), CASE_LOWER)), 'device' => ['architecture' => $this->getDeviceArchitecture($filteredHeaders), 'deviceName' => $device->getDeviceName(), 'marketingName' => $device->getMarketingName(), 'manufacturer' => $device->getManufacturer()->getType(), 'brand' => $device->getBrand()->getType(), 'dualOrientation' => $device->getDualOrientation(), 'simCount' => $device->getSimCount(), 'display' => $device->getDisplay()->toArray(), 'type' => $device->getType()->getType(), 'ismobile' => $deviceIsMobile ?? $device->getType()->isMobile(), 'istv' => $device->getType()->isTv(), 'bits' => $this->getDeviceBitness($filteredHeaders)], 'os' => ['name' => $platformName, 'marketingName' => $platformMarketingName, 'version' => $platform->getVersion()->getVersion(), 'manufacturer' => $platform->getManufacturer()->getType()], 'client' => ['name' => $client->getName(), 'version' => $client->getVersion()->getVersion(), 'manufacturer' => $client->getManufacturer()->getType(), 'type' => $client->getType()->getType(), 'isbot' => $client->getType()->isBot()], 'engine' => ['name' => $engine->getName(), 'version' => $engine->getVersion()->getVersion(), 'manufacturer' => $engine->getManufacturer()->getType()]]; + return ['headers' => array_map(callback: static fn(HeaderInterface $header): string => $header->getValue(), array: $request->getHeaders()), 'device' => ['architecture' => $this->getDeviceArchitecture($filteredHeaders), 'deviceName' => $device->getDeviceName(), 'marketingName' => $device->getMarketingName(), 'manufacturer' => $device->getManufacturer()->getType(), 'brand' => $device->getBrand()->getType(), 'dualOrientation' => $device->getDualOrientation(), 'simCount' => $device->getSimCount(), 'display' => $device->getDisplay()->toArray(), 'type' => $device->getType()->getType(), 'ismobile' => $deviceIsMobile ?? $device->getType()->isMobile(), 'istv' => $device->getType()->isTv(), 'bits' => $this->getDeviceBitness($filteredHeaders)], 'os' => ['name' => $platformName, 'marketingName' => $platformMarketingName, 'version' => $platform->getVersion()->getVersion(), 'manufacturer' => $platform->getManufacturer()->getType()], 'client' => ['name' => $client->getName(), 'version' => $client->getVersion()->getVersion(), 'manufacturer' => $client->getManufacturer()->getType(), 'type' => $client->getType()->getType(), 'isbot' => $client->getType()->isBot()], 'engine' => ['name' => $engine->getName(), 'version' => $engine->getVersion()->getVersion(), 'manufacturer' => $engine->getManufacturer()->getType()]]; } /** @throws NotNumericException */ private function getVersion(string|null $inputVersion): VersionInterface

Check warning on line 187 in src/Detector.php

View workflow job for this annotation

GitHub Actions / UnitTests / Code Coverage with PHPUnit (ubuntu-24.04, 8.3, lowest)

Escaped Mutant for Mutator "UnwrapArrayChangeKeyCase": @@ @@ // }, // array: $request->getHeaders() // )); - return ['headers' => array_map(callback: static fn(HeaderInterface $header): string => $header->getValue(), array: array_change_key_case($request->getHeaders(), CASE_LOWER)), 'device' => ['architecture' => $this->getDeviceArchitecture($filteredHeaders), 'deviceName' => $device->getDeviceName(), 'marketingName' => $device->getMarketingName(), 'manufacturer' => $device->getManufacturer()->getType(), 'brand' => $device->getBrand()->getType(), 'dualOrientation' => $device->getDualOrientation(), 'simCount' => $device->getSimCount(), 'display' => $device->getDisplay()->toArray(), 'type' => $device->getType()->getType(), 'ismobile' => $deviceIsMobile ?? $device->getType()->isMobile(), 'istv' => $device->getType()->isTv(), 'bits' => $this->getDeviceBitness($filteredHeaders)], 'os' => ['name' => $platformName, 'marketingName' => $platformMarketingName, 'version' => $platform->getVersion()->getVersion(), 'manufacturer' => $platform->getManufacturer()->getType()], 'client' => ['name' => $client->getName(), 'version' => $client->getVersion()->getVersion(), 'manufacturer' => $client->getManufacturer()->getType(), 'type' => $client->getType()->getType(), 'isbot' => $client->getType()->isBot()], 'engine' => ['name' => $engine->getName(), 'version' => $engine->getVersion()->getVersion(), 'manufacturer' => $engine->getManufacturer()->getType()]]; + return ['headers' => array_map(callback: static fn(HeaderInterface $header): string => $header->getValue(), array: $request->getHeaders()), 'device' => ['architecture' => $this->getDeviceArchitecture($filteredHeaders), 'deviceName' => $device->getDeviceName(), 'marketingName' => $device->getMarketingName(), 'manufacturer' => $device->getManufacturer()->getType(), 'brand' => $device->getBrand()->getType(), 'dualOrientation' => $device->getDualOrientation(), 'simCount' => $device->getSimCount(), 'display' => $device->getDisplay()->toArray(), 'type' => $device->getType()->getType(), 'ismobile' => $deviceIsMobile ?? $device->getType()->isMobile(), 'istv' => $device->getType()->isTv(), 'bits' => $this->getDeviceBitness($filteredHeaders)], 'os' => ['name' => $platformName, 'marketingName' => $platformMarketingName, 'version' => $platform->getVersion()->getVersion(), 'manufacturer' => $platform->getManufacturer()->getType()], 'client' => ['name' => $client->getName(), 'version' => $client->getVersion()->getVersion(), 'manufacturer' => $client->getManufacturer()->getType(), 'type' => $client->getType()->getType(), 'isbot' => $client->getType()->isBot()], 'engine' => ['name' => $engine->getName(), 'version' => $engine->getVersion()->getVersion(), 'manufacturer' => $engine->getManufacturer()->getType()]]; } /** @throws NotNumericException */ private function getVersion(string|null $inputVersion): VersionInterface
),
'device' => [
Expand Down
5 changes: 1 addition & 4 deletions src/DetectorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
use Psr\Log\LoggerInterface;
use Psr\SimpleCache\CacheInterface as PsrCacheInterface;
use RuntimeException;
use UaBrowserType\TypeLoader;
use UaNormalizer\NormalizerFactory;
use UaRequest\RequestBuilder;

Expand Down Expand Up @@ -103,9 +102,7 @@ public function __invoke(): Detector
engineLoader: $engineLoader,
);

$requestBuilder = new RequestBuilder(
headerLoader: $headerLoader
);
$requestBuilder = new RequestBuilder(headerLoader: $headerLoader);

$this->detector = new Detector(
logger: $this->logger,
Expand Down
5 changes: 1 addition & 4 deletions src/Loader/BrowserLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use UaLoader\Data\ClientDataInterface;
use UaLoader\Exception\NotFoundException;
use UaResult\Browser\Browser;
use UaResult\Browser\BrowserInterface;
use UaResult\Company\Company;

use function array_key_exists;
Expand All @@ -50,9 +49,7 @@ public function __construct(
$initData();
}

/**
* @throws NotFoundException
*/
/** @throws NotFoundException */
#[Override]
public function load(string $key, string $useragent = ''): ClientDataInterface
{
Expand Down
42 changes: 19 additions & 23 deletions src/Loader/Data/ClientData.php
Original file line number Diff line number Diff line change
@@ -1,44 +1,40 @@
<?php

/**
* This file is part of the browser-detector package.
*
* Copyright (c) 2012-2025, Thomas Mueller <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types = 1);

namespace BrowserDetector\Loader\Data;

use Override;
use UaLoader\Data\ClientDataInterface;
use UaResult\Browser\BrowserInterface;

class ClientData implements ClientDataInterface
final readonly class ClientData implements ClientDataInterface
{
private BrowserInterface $client;
private string|null $engine;

/**
* @param BrowserInterface $client
* @param string|null $engine
* @throws void
*/
public function __construct(BrowserInterface $client, ?string $engine)
/** @throws void */
public function __construct(private BrowserInterface $client, private string | null $engine)
{
$this->client = $client;
$this->engine = $engine;
}

/**
* @return BrowserInterface
* @throws void
*/
/** @throws void */
#[Override]
public function getClient(): BrowserInterface
{
return $this->client;
}

/**
* @return string|null
* @throws void
*/
public function getEngine(): string|null
/** @throws void */
#[Override]
public function getEngine(): string | null
{
return $this->engine;
}

}
}
42 changes: 19 additions & 23 deletions src/Loader/Data/DeviceData.php
Original file line number Diff line number Diff line change
@@ -1,44 +1,40 @@
<?php

/**
* This file is part of the browser-detector package.
*
* Copyright (c) 2012-2025, Thomas Mueller <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types = 1);

namespace BrowserDetector\Loader\Data;

use Override;
use UaLoader\Data\DeviceDataInterface;
use UaResult\Device\DeviceInterface;

class DeviceData implements DeviceDataInterface
final readonly class DeviceData implements DeviceDataInterface
{
private DeviceInterface $device;
private string|null $os;

/**
* @param DeviceInterface $device
* @param string|null $os
* @throws void
*/
public function __construct(DeviceInterface $device, ?string $os)
/** @throws void */
public function __construct(private DeviceInterface $device, private string | null $os)
{
$this->device = $device;
$this->os = $os;
}

/**
* @return DeviceInterface
* @throws void
*/
/** @throws void */
#[Override]
public function getDevice(): DeviceInterface
{
return $this->device;
}

/**
* @return string|null
* @throws void
*/
public function getOs(): string|null
/** @throws void */
#[Override]
public function getOs(): string | null
{
return $this->os;
}

}
}
11 changes: 2 additions & 9 deletions src/Loader/DeviceLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
use RuntimeException;
use stdClass;
use UaDeviceType\Type;
use UaDeviceType\TypeLoaderInterface;
use UaDeviceType\Unknown;
use UaLoader\Data\DeviceDataInterface;
use UaLoader\DeviceLoaderInterface;
use UaLoader\Exception\NotFoundException;
Expand All @@ -44,9 +42,7 @@ public function __construct(
$initData();
}

/**
* @throws NotFoundException
*/
/** @throws NotFoundException */
#[Override]
public function load(string $key): DeviceDataInterface
{
Expand All @@ -69,10 +65,7 @@ public function load(string $key): DeviceDataInterface
'"platform" property is required',
);

return new DeviceData(
device: $device,
os: $deviceData->platform,
);
return new DeviceData(device: $device, os: $deviceData->platform);
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Loader/DeviceLoaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Override;
use Psr\Log\LoggerInterface;
use RuntimeException;
use UaDeviceType\TypeLoader;
use UaLoader\DeviceLoaderInterface;

use function array_key_exists;
Expand Down
8 changes: 4 additions & 4 deletions src/Parser/Header/BaiduFlyflow.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

/**
* This file is part of the mimmi20/ua-generic-request package.
* This file is part of the browser-detector package.
*
* Copyright (c) 2015-2025, Thomas Mueller <[email protected]>
* Copyright (c) 2012-2025, Thomas Mueller <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand All @@ -19,10 +19,10 @@

use function preg_match;

final class BaiduFlyflow implements DeviceCodeInterface
final readonly class BaiduFlyflow implements DeviceCodeInterface
{
/** @throws void */
public function __construct(private readonly DeviceParserInterface $deviceParser)
public function __construct(private DeviceParserInterface $deviceParser)
{
// nothing to do
}
Expand Down
55 changes: 55 additions & 0 deletions src/Parser/Header/CrawledByClientCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* This file is part of the browser-detector package.
*
* Copyright (c) 2012-2025, Thomas Mueller <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types = 1);

namespace BrowserDetector\Parser\Header;

use Override;
use UaParser\ClientCodeInterface;

use function array_key_first;
use function mb_strtolower;
use function str_contains;

final class CrawledByClientCode implements ClientCodeInterface
{
use SortTrait;

/** @throws void */
#[Override]
public function hasClientCode(string $value): bool
{
$list = $this->sort($value);

if ($list === null || $list === []) {
return false;
}

$key = array_key_first($list);
$code = mb_strtolower($key);

Check warning on line 38 in src/Parser/Header/CrawledByClientCode.php

View check run for this annotation

Codecov / codecov/patch

src/Parser/Header/CrawledByClientCode.php#L37-L38

Added lines #L37 - L38 were not covered by tests

return !str_contains($code, 'brand') && $code !== 'chromium';

Check warning on line 40 in src/Parser/Header/CrawledByClientCode.php

View check run for this annotation

Codecov / codecov/patch

src/Parser/Header/CrawledByClientCode.php#L40

Added line #L40 was not covered by tests
}

/**
* @return non-empty-string|null
*
* @throws void
*
* @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
*/
#[Override]

Check warning on line 50 in src/Parser/Header/CrawledByClientCode.php

View check run for this annotation

Codecov / codecov/patch

src/Parser/Header/CrawledByClientCode.php#L50

Added line #L50 was not covered by tests
public function getClientCode(string $value): string | null
{
return null;

Check warning on line 53 in src/Parser/Header/CrawledByClientCode.php

View check run for this annotation

Codecov / codecov/patch

src/Parser/Header/CrawledByClientCode.php#L53

Added line #L53 was not covered by tests
}
}
Loading

0 comments on commit 37b8649

Please sign in to comment.