Skip to content

Commit c85d4d5

Browse files
author
Renan de Lima
authored
Merge pull request #35 from acsiomatic/create-proxy-callable-phpstan
Extract callables to dedicated methods
2 parents 9a4a658 + 7d5314b commit c85d4d5

2 files changed

Lines changed: 68 additions & 39 deletions

File tree

.github/workflows/phpunit.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,11 @@ jobs:
4545
php-version: ${{ matrix.php }}
4646
coverage: none
4747

48+
- name: Require specific dependencies versions
49+
run: composer require matomo/device-detector:${{ matrix.dd }} symfony/framework-bundle:${{ matrix.sf }}
50+
4851
- name: Install Composer dependencies
4952
uses: ramsey/composer-install@v2
50-
with:
51-
composer-options: >
52-
--with matomo/device-detector:${{ matrix.dd }}
53-
--with symfony/framework-bundle:${{ matrix.sf }}
5453

5554
- name: Run PHPUnit
5655
run: vendor/bin/phpunit --testdox

src/Factory/DeviceDetectorProxyFactory.php

Lines changed: 65 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use ProxyManager\Factory\AccessInterceptorValueHolderFactory;
88
use ProxyManager\FileLocator\FileLocator;
99
use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy;
10+
use ProxyManager\Proxy\AccessInterceptorInterface;
1011

1112
/**
1213
* @internal
@@ -35,44 +36,33 @@ public function getProxyDir(): string
3536

3637
public function createDeviceDetectorProxy(): DeviceDetector
3738
{
38-
$parseCaller = static function (
39-
DeviceDetector $proxy,
40-
DeviceDetector $real
41-
): void {
42-
if (!$real->isParsed()) {
43-
$real->parse();
44-
}
45-
};
39+
$configuration = $this->createConfiguration();
4640

47-
$magicParseCaller = static function (
48-
DeviceDetector $proxy,
49-
DeviceDetector $real,
50-
string $method,
51-
array $arguments
52-
): void {
53-
if (!$real->isParsed() && 0 === stripos($arguments['methodName'] ?? '', 'is')) {
54-
$real->parse();
55-
}
56-
};
41+
$instance = new DeviceDetector();
5742

58-
$configuration = $this->createConfiguration();
59-
$factory = new AccessInterceptorValueHolderFactory($configuration);
43+
$autoParserForMagicCall = $this->buildAutoParserCallerForMagicCall();
44+
$autoParser = $this->buildAutoParserCaller();
45+
$prefixInterceptors = [
46+
'__call' => $autoParserForMagicCall,
47+
'getBot' => $autoParser,
48+
'getBrand' => $autoParser,
49+
'getBrandName' => $autoParser,
50+
'getClient' => $autoParser,
51+
'getDevice' => $autoParser,
52+
'getDeviceName' => $autoParser,
53+
'getModel' => $autoParser,
54+
'getOs' => $autoParser,
55+
'isBot' => $autoParser,
56+
'isBrowser' => $autoParser,
57+
'isDesktop' => $autoParser,
58+
'isMobile' => $autoParser,
59+
];
6060

61-
return $factory->createProxy(new DeviceDetector(), [
62-
'__call' => $magicParseCaller,
63-
'getBot' => $parseCaller,
64-
'getBrand' => $parseCaller,
65-
'getBrandName' => $parseCaller,
66-
'getClient' => $parseCaller,
67-
'getDevice' => $parseCaller,
68-
'getDeviceName' => $parseCaller,
69-
'getModel' => $parseCaller,
70-
'getOs' => $parseCaller,
71-
'isBot' => $parseCaller,
72-
'isBrowser' => $parseCaller,
73-
'isDesktop' => $parseCaller,
74-
'isMobile' => $parseCaller,
75-
]);
61+
return (new AccessInterceptorValueHolderFactory($configuration))
62+
->createProxy(
63+
$instance,
64+
$prefixInterceptors
65+
);
7666
}
7767

7868
private function createConfiguration(): Configuration
@@ -86,4 +76,44 @@ private function createConfiguration(): Configuration
8676

8777
return $config;
8878
}
79+
80+
/**
81+
* @return callable(
82+
* AccessInterceptorInterface<DeviceDetector> $proxy,
83+
* DeviceDetector $real,
84+
* string $method,
85+
* array $arguments,
86+
* )
87+
*/
88+
private function buildAutoParserCallerForMagicCall(): callable
89+
{
90+
return static function (
91+
AccessInterceptorInterface $proxy,
92+
DeviceDetector $real,
93+
string $method,
94+
array $arguments
95+
): void {
96+
if (!$real->isParsed() && 0 === stripos($arguments['methodName'] ?? '', 'is')) {
97+
$real->parse();
98+
}
99+
};
100+
}
101+
102+
/**
103+
* @return callable(
104+
* AccessInterceptorInterface<DeviceDetector> $proxy,
105+
* DeviceDetector $real,
106+
* )
107+
*/
108+
private function buildAutoParserCaller(): callable
109+
{
110+
return static function (
111+
AccessInterceptorInterface $proxy,
112+
DeviceDetector $real
113+
): void {
114+
if (!$real->isParsed()) {
115+
$real->parse();
116+
}
117+
};
118+
}
89119
}

0 commit comments

Comments
 (0)