Skip to content

Commit 4a21fc2

Browse files
tattalidigifa
authored andcommitted
Version for PHP 8.2 and Symfony 6.4
2 parents 9621ed5 + 116401c commit 4a21fc2

File tree

15 files changed

+225
-237
lines changed

15 files changed

+225
-237
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ lint:
88
php vendor/bin/phpcs --report=code
99
php vendor/bin/phpstan analyse
1010

11+
refactor:
12+
php vendor/bin/php-cs-fixer fix
13+
php vendor/bin/rector
14+
1115
test: lint phpunit

composer.json

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tattali/mobile-detect-bundle",
3-
"description": "Symfony 3.4-6.x bundle to detect mobile devices, manage mobile view and redirect to the mobile and tablet version.",
3+
"description": "Symfony 6.4 bundle to detect mobile devices, manage mobile view and redirect to the mobile and tablet version.",
44
"keywords": [
55
"mobile detect",
66
"device detect",
@@ -34,24 +34,25 @@
3434
}
3535
],
3636
"require": {
37-
"php": ">=7.2.5",
38-
"mobiledetect/mobiledetectlib": "^2.8.15",
39-
"symfony/dependency-injection": "^5.0 || ^6.0",
40-
"symfony/event-dispatcher": "^5.0 || ^6.0",
41-
"symfony/framework-bundle": "^5.0 || ^6.0",
42-
"symfony/yaml": "^5.0 || ^6.0",
37+
"php": ">=8.2",
38+
"mobiledetect/mobiledetectlib": "^4.8",
39+
"symfony/dependency-injection": "^6.4",
40+
"symfony/event-dispatcher": "^6.4",
41+
"symfony/framework-bundle": "^6.4",
42+
"symfony/yaml": "^6.4",
4343
"twig/twig": "^2.0 || ^3.0"
4444
},
4545
"require-dev": {
4646
"friendsofphp/php-cs-fixer": "^3.4 || ^3.8",
4747
"phpstan/extension-installer": "^1.0",
48+
"phpstan/phpstan": "^1.0",
4849
"phpstan/phpstan-nette": "^1.0",
4950
"phpstan/phpstan-phpunit": "^1.0",
50-
"phpstan/phpstan": "^1.0",
51-
"phpunit/phpunit": "^8.5 || ^9.5",
51+
"phpunit/phpunit": "^9.5",
52+
"rector/rector": "^1.2",
5253
"squizlabs/php_codesniffer": "^3.6",
53-
"symfony/dotenv": "^5.0 || ^6.0",
54-
"symfony/phpunit-bridge": "^5.0 || ^6.0"
54+
"symfony/dotenv": "^6.4",
55+
"symfony/phpunit-bridge": "^6.4"
5556
},
5657
"config": {
5758
"sort-packages": true,

rector.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
7+
return RectorConfig::configure()
8+
->withPaths([
9+
__DIR__ . '/src',
10+
__DIR__ . '/tests',
11+
])
12+
->withPhpSets(php82: true)
13+
->withAttributesSets(symfony: true, doctrine: true)
14+
->withSets([
15+
Rector\Symfony\Set\SymfonySetList::SYMFONY_50,
16+
Rector\Symfony\Set\SymfonySetList::SYMFONY_50_TYPES,
17+
Rector\Symfony\Set\SymfonySetList::SYMFONY_51,
18+
Rector\Symfony\Set\SymfonySetList::SYMFONY_52,
19+
Rector\Symfony\Set\SymfonySetList::SYMFONY_52_VALIDATOR_ATTRIBUTES,
20+
Rector\Symfony\Set\SymfonySetList::SYMFONY_53,
21+
Rector\Symfony\Set\SymfonySetList::SYMFONY_60,
22+
Rector\Symfony\Set\SymfonySetList::SYMFONY_61,
23+
Rector\Symfony\Set\SymfonySetList::SYMFONY_62,
24+
Rector\Symfony\Set\SymfonySetList::SYMFONY_63,
25+
Rector\Symfony\Set\SymfonySetList::SYMFONY_64,
26+
Rector\Symfony\Set\SymfonySetList::SYMFONY_CODE_QUALITY,
27+
Rector\Symfony\Set\SymfonySetList::SYMFONY_CONSTRUCTOR_INJECTION,
28+
])
29+
->withDeadCodeLevel(10)
30+
->withTypeCoverageLevel(10);

src/DataCollector/DeviceDataCollector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function __construct(DeviceView $deviceView)
4545
public function collect(
4646
Request $request,
4747
Response $response,
48-
\Throwable $exception = null
48+
?\Throwable $exception = null,
4949
): void {
5050
$this->data['currentView'] = $this->deviceView->getViewType();
5151
$this->data['views'] = [
@@ -127,7 +127,7 @@ protected function canUseView(string $view, ?string $host): bool
127127
&& !empty($this->redirectConfig[$view]['host'])
128128
&& \in_array($this->redirectConfig[$view]['action'], [RequestResponseListener::REDIRECT, RequestResponseListener::REDIRECT_WITHOUT_PATH], true)
129129
) {
130-
$parseHost = parse_url($this->redirectConfig[$view]['host']);
130+
$parseHost = parse_url((string) $this->redirectConfig[$view]['host']);
131131
$redirectHost = $parseHost['scheme'].'://'.$parseHost['host'];
132132
if (!empty($parseHost['port'])) {
133133
$redirectHost .= ':'.$parseHost['port'];
@@ -143,7 +143,7 @@ protected function canUseView(string $view, ?string $host): bool
143143

144144
private function generateSwitchLink(
145145
Request $request,
146-
string $view
146+
string $view,
147147
): ?string {
148148
$requestSwitchView = $request->duplicate();
149149
$requestSwitchView->query->set($this->deviceView->getSwitchParam(), $view);

src/DependencyInjection/MobileDetectExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
use Symfony\Component\Config\FileLocator;
1717
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
use Symfony\Component\DependencyInjection\Extension\Extension;
1819
use Symfony\Component\DependencyInjection\Loader;
19-
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
2020

2121
class MobileDetectExtension extends Extension
2222
{

src/DeviceDetector/MobileDetector.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,24 @@
1313

1414
namespace MobileDetectBundle\DeviceDetector;
1515

16+
use Detection\MobileDetect;
17+
1618
/**
1719
* @author suncat2000 <nikolay.kotovsky@gmail.com>
1820
*/
19-
class MobileDetector extends \Mobile_Detect implements MobileDetectorInterface
21+
class MobileDetector extends MobileDetect implements MobileDetectorInterface
2022
{
23+
public const VERSION_TYPE_STRING = 'text';
24+
25+
public const VERSION_TYPE_FLOAT = 'float';
26+
27+
public static function getUserAgents(): array
28+
{
29+
return self::getBrowsers();
30+
}
31+
32+
public function getCfHeaders(): array
33+
{
34+
return $this->getCloudFrontHttpHeaders();
35+
}
2136
}

src/DeviceDetector/MobileDetectorInterface.php

Lines changed: 25 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,6 @@
196196
* @method bool isNetFront()
197197
* @method bool isGenericBrowser()
198198
* @method bool isPaleMoon()
199-
* @method bool isBot()
200-
* @method bool isMobileBot()
201-
* @method bool isDesktopMode()
202-
* @method bool isTV()
203199
* @method bool isWebKit()
204200
* @method bool isConsole()
205201
* @method bool isWatch()
@@ -211,97 +207,61 @@ interface MobileDetectorInterface
211207
*
212208
* @method bool is[...]()
213209
*
214-
* @param string $name
215-
* @param array $arguments
216-
*
217210
* @throws \BadMethodCallException when the method doesn't exist and doesn't start with 'is'
218211
*/
219-
public function __call($name, $arguments);
212+
public function __call(string $name, array $arguments);
220213

221214
/**
222215
* Retrieve the list of known browsers. Specifically, the user agents.
223216
*
224217
* @return array list of browsers / user agents
225218
*/
226-
public static function getBrowsers();
219+
public static function getBrowsers(): array;
227220

228221
/**
229222
* Retrieve the list of mobile operating systems.
230223
*
231224
* @return array the list of mobile operating systems
232225
*/
233-
public static function getOperatingSystems();
226+
public static function getOperatingSystems(): array;
234227

235228
/**
236229
* Retrieve the list of known phone devices.
237230
*
238231
* @return array list of phone devices
239232
*/
240-
public static function getPhoneDevices();
233+
public static function getPhoneDevices(): array;
241234

242235
/**
243236
* Get the properties array.
244-
*
245-
* @return array
246237
*/
247-
public static function getProperties();
248-
249-
/**
250-
* Get the current script version.
251-
* This is useful for the demo.php file,
252-
* so people can check on what version they are testing
253-
* for mobile devices.
254-
*
255-
* @return string the version number in semantic version format
256-
*/
257-
public static function getScriptVersion();
238+
public static function getProperties(): array;
258239

259240
/**
260241
* Retrieve the list of known tablet devices.
261242
*
262243
* @return array list of tablet devices
263244
*/
264-
public static function getTabletDevices();
245+
public static function getTabletDevices(): array;
265246

266247
/**
267248
* Alias for getBrowsers() method.
268249
*
269250
* @return array list of user agents
270251
*/
271-
public static function getUserAgents();
272-
273-
/**
274-
* Retrieve the list of known utilities.
275-
*
276-
* @return array list of utilities
277-
*/
278-
public static function getUtilities();
252+
public static function getUserAgents(): array;
279253

280254
/**
281255
* Check the HTTP headers for signs of mobile.
282256
* This is the fastest mobile check possible; it's used
283257
* inside isMobile() method.
284-
*
285-
* @return bool
286258
*/
287-
public function checkHttpHeadersForMobile();
259+
public function checkHttpHeadersForMobile(): bool;
288260

289261
/**
290262
* Retrieves the cloudfront headers.
291-
*
292-
* @return array
293263
*/
294-
public function getCfHeaders();
295-
296-
/**
297-
* Set CloudFront headers
298-
* http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/header-caching.html#header-caching-web-device.
299-
*
300-
* @param array $cfHeaders List of HTTP headers
301-
*
302-
* @return bool If there were CloudFront headers to be set
303-
*/
304-
public function setCfHeaders($cfHeaders = null);
264+
public function getCfHeaders(): array;
305265

306266
/**
307267
* Retrieves a particular header. If it doesn't exist, no exception/error is caused.
@@ -313,78 +273,67 @@ public function setCfHeaders($cfHeaders = null);
313273
*
314274
* @return string|null the value of the header
315275
*/
316-
public function getHttpHeader($header);
276+
public function getHttpHeader(string $header): ?string;
317277

318278
/**
319279
* Retrieves the HTTP headers.
320-
*
321-
* @return array
322280
*/
323-
public function getHttpHeaders();
281+
public function getHttpHeaders(): array;
324282

325283
/**
326284
* Set the HTTP Headers. Must be PHP-flavored. This method will reset existing headers.
327285
*
328-
* @param array $httpHeaders The headers to set. If null, then using PHP's _SERVER to extract
329-
* the headers. The default null is left for backwards compatibility.
286+
* @param array $httpHeaders the headers to set
330287
*/
331-
public function setHttpHeaders($httpHeaders = null);
288+
public function setHttpHeaders(array $httpHeaders = []): void;
332289

333-
public function getMatchesArray();
290+
public function getMatchesArray(): ?array;
334291

335-
public function getMatchingRegex();
292+
public function getMatchingRegex(): ?string;
336293

337-
public function getMobileHeaders();
294+
public function getMobileHeaders(): array;
338295

339296
/**
340297
* Get all possible HTTP headers that
341298
* can contain the User-Agent string.
342299
*
343300
* @return array list of HTTP headers
344301
*/
345-
public function getUaHttpHeaders();
302+
public function getUaHttpHeaders(): array;
346303

347304
/**
348305
* Retrieve the User-Agent.
349306
*
350307
* @return string|null the user agent if it's set
351308
*/
352-
public function getUserAgent();
309+
public function getUserAgent(): ?string;
353310

354311
/**
355312
* Set the User-Agent to be used.
356313
*
357314
* @param string $userAgent the user agent string to set
358-
*
359-
* @return string|null
360315
*/
361-
public function setUserAgent($userAgent = null);
316+
public function setUserAgent(string $userAgent): string;
362317

363318
/**
364319
* This method checks for a certain property in the
365320
* userAgent.
366321
*
367-
* @param string $key
368-
*
369322
* @return bool|int|null
370323
*/
371-
public function is($key);
324+
public function is(string $key);
372325

373326
/**
374327
* Check if the device is mobile.
375328
* Returns true if any type of mobile device detected, including special ones.
376-
*
377-
* @return bool
378329
*/
379-
public function isMobile();
330+
public function isMobile(): bool;
380331

381332
/**
382333
* Check if the device is a tablet.
383334
* Return true if any type of tablet device is detected.
384-
*
385-
* @return bool
386335
*/
387-
public function isTablet();
336+
public function isTablet(): bool;
388337

389338
/**
390339
* Some detection rules are relative (not standard),
@@ -394,21 +343,15 @@ public function isTablet();
394343
*
395344
* This method will be used to check custom regexes against
396345
* the User-Agent string.
397-
*
398-
* @param string $userAgent
399-
*
400-
* @return bool
401346
*/
402-
public function match($regex, $userAgent = null);
347+
public function match(string $regex, string $userAgent): bool;
403348

404349
/**
405350
* Prepare the version number.
406351
*
407352
* @param string $ver The string version, like "2.6.21.2152";
408-
*
409-
* @return float
410353
*/
411-
public function prepareVersionNo($ver);
354+
public function prepareVersionNo(string $ver): float;
412355

413356
/**
414357
* Check the version of the given property in the User-Agent.
@@ -423,5 +366,5 @@ public function prepareVersionNo($ver);
423366
*
424367
* @return string|float the version of the property we are trying to extract
425368
*/
426-
public function version($propertyName, $type = MobileDetector::VERSION_TYPE_STRING);
369+
public function version(string $propertyName, string $type = MobileDetector::VERSION_TYPE_STRING): float|bool|string;
427370
}

0 commit comments

Comments
 (0)