Skip to content

Commit a3ed090

Browse files
author
Bartłomiej Nowak
committed
coding standards fix
1 parent 9ac24b5 commit a3ed090

10 files changed

+291
-272
lines changed

src/Symfony/Message.php

+31-29
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
1-
<?php
2-
3-
declare(strict_types=1);
1+
<?php declare(strict_types = 1);
42

53
namespace PHPStan\Symfony;
64

5+
use function count;
6+
77
final class Message
88
{
9-
/** @var string */
10-
private $class;
11-
12-
/** @var array */
13-
private $returnTypes;
14-
15-
public function __construct(string $class, array $returnTypes)
16-
{
17-
$this->class = $class;
18-
$this->returnTypes = $returnTypes;
19-
}
20-
21-
public function getClass(): string
22-
{
23-
return $this->class;
24-
}
25-
26-
public function getReturnTypes(): array
27-
{
28-
return $this->returnTypes;
29-
}
30-
31-
public function countReturnTypes(): int
32-
{
33-
return count($this->returnTypes);
34-
}
9+
10+
/** @var string */
11+
private $class;
12+
13+
/** @var array */
14+
private $returnTypes;
15+
16+
public function __construct(string $class, array $returnTypes)
17+
{
18+
$this->class = $class;
19+
$this->returnTypes = $returnTypes;
20+
}
21+
22+
public function getClass(): string
23+
{
24+
return $this->class;
25+
}
26+
27+
public function getReturnTypes(): array
28+
{
29+
return $this->returnTypes;
30+
}
31+
32+
public function countReturnTypes(): int
33+
{
34+
return count($this->returnTypes);
35+
}
36+
3537
}

src/Symfony/MessageMap.php

+27-25
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
1-
<?php
2-
3-
declare(strict_types=1);
1+
<?php declare(strict_types = 1);
42

53
namespace PHPStan\Symfony;
64

5+
use function array_key_exists;
6+
77
final class MessageMap
88
{
9-
/** @var Message[] */
10-
private $messages = [];
11-
12-
/**
13-
* @param Message[] $messages
14-
*/
15-
public function __construct(array $messages)
16-
{
17-
foreach ($messages as $message) {
18-
$this->messages[$message->getClass()] = $message;
19-
}
20-
}
21-
22-
public function getMessageForClass(string $class): ?Message
23-
{
24-
return $this->messages[$class] ?? null;
25-
}
26-
27-
public function hasMessageForClass(string $class): bool
28-
{
29-
return array_key_exists($class, $this->messages);
30-
}
9+
10+
/** @var Message[] */
11+
private $messages = [];
12+
13+
/**
14+
* @param Message[] $messages
15+
*/
16+
public function __construct(array $messages)
17+
{
18+
foreach ($messages as $message) {
19+
$this->messages[$message->getClass()] = $message;
20+
}
21+
}
22+
23+
public function getMessageForClass(string $class): ?Message
24+
{
25+
return $this->messages[$class] ?? null;
26+
}
27+
28+
public function hasMessageForClass(string $class): bool
29+
{
30+
return array_key_exists($class, $this->messages);
31+
}
32+
3133
}

src/Symfony/MessageMapFactory.php

+117-110
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php declare(strict_types=1);
1+
<?php declare(strict_types = 1);
22

33
namespace PHPStan\Symfony;
44

@@ -7,118 +7,125 @@
77
use PHPStan\Reflection\ReflectionProvider;
88
use PHPStan\Type\ObjectType;
99
use PHPStan\Type\UnionType;
10+
use RuntimeException;
1011
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;
12+
use function count;
13+
use function is_int;
14+
use function is_null;
15+
use function is_string;
1116

1217
// todo add tests
1318
final class MessageMapFactory
1419
{
15-
/** @var ReflectionProvider */
16-
private $reflectionProvider;
17-
18-
/** @var ServiceMap */
19-
private $serviceMap;
20-
21-
public function __construct(ServiceMap $symfonyServiceMap, ReflectionProvider $reflectionProvider)
22-
{
23-
$this->serviceMap = $symfonyServiceMap;
24-
$this->reflectionProvider = $reflectionProvider;
25-
}
26-
27-
public function create(): MessageMap
28-
{
29-
$returnTypesMap = [];
30-
31-
foreach ($this->serviceMap->getServices() as $service) {
32-
$serviceClass = $service->getClass();
33-
34-
// todo handle abstract/parent services somehow?
35-
if (is_null($serviceClass)) {
36-
continue;
37-
}
38-
39-
foreach ($service->getTags() as $tag) {
40-
// todo could there be more tags with the same name for the same service?
41-
// todo check if message handler tag name is constant or configurable
42-
if ($tag->getName() !== 'messenger.message_handler') {
43-
continue;
44-
}
45-
46-
$tagAttributes = $tag->getAttributes();
47-
$reflectionClass = $this->reflectionProvider->getClass($serviceClass);
48-
49-
if (isset($tagAttributes['handles'])) {
50-
$handles = isset($tag['method']) ? [$tag['handles'] => $tag['method']] : [$tag['handles']];
51-
} else {
52-
$handles = $this->guessHandledMessages($reflectionClass);
53-
}
54-
55-
foreach ($handles as $messageClassName => $options) {
56-
if (\is_int($messageClassName) && \is_string($options)) {
57-
$messageClassName = $options;
58-
$options = [];
59-
}
60-
61-
$options['method'] = $options['method'] ?? '__invoke';
62-
63-
$methodReflection = $reflectionClass->getNativeMethod($options['method']);
64-
$variant = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
65-
66-
$returnTypesMap[$messageClassName][] = $variant->getReturnType();
67-
}
68-
}
69-
}
70-
71-
$messages = [];
72-
foreach ($returnTypesMap as $messageClassName => $returnTypes) {
73-
$messages[] = new Message($messageClassName, $returnTypes);
74-
}
75-
76-
return new MessageMap($messages);
77-
}
78-
79-
private function guessHandledMessages(ClassReflection $reflectionClass): iterable
80-
{
81-
if ($reflectionClass->implementsInterface(MessageSubscriberInterface::class)) {
82-
// todo handle different return formats
83-
return $reflectionClass->getName()::getHandledMessages();
84-
}
85-
86-
// todo handle if doesn't exists
87-
$methodReflection = $reflectionClass->getNativeMethod('__invoke');
88-
89-
$variant = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
90-
$parameters = $variant->getParameters();
91-
92-
if (1 !== count($parameters)) {
93-
// todo handle error
94-
throw new \RuntimeException('invalid handler');
95-
}
96-
97-
$type = $parameters[0]->getType();
98-
99-
if ($type instanceof UnionType) {
100-
$types = [];
101-
foreach ($type->getTypes() as $type) {
102-
if (!$type instanceof ObjectType) {
103-
// todo handle error
104-
throw new \RuntimeException('invalid handler');
105-
}
106-
107-
$types[] = $type->getClassName();
108-
}
109-
110-
if ($types) {
111-
return $types;
112-
}
113-
114-
// todo handle error
115-
throw new \RuntimeException('invalid handler');
116-
}
117-
118-
if (!$type instanceof ObjectType) {
119-
throw new \RuntimeException('invalid handler');
120-
}
121-
122-
return [$type->getClassName()];
123-
}
20+
21+
/** @var ReflectionProvider */
22+
private $reflectionProvider;
23+
24+
/** @var ServiceMap */
25+
private $serviceMap;
26+
27+
public function __construct(ServiceMap $symfonyServiceMap, ReflectionProvider $reflectionProvider)
28+
{
29+
$this->serviceMap = $symfonyServiceMap;
30+
$this->reflectionProvider = $reflectionProvider;
31+
}
32+
33+
public function create(): MessageMap
34+
{
35+
$returnTypesMap = [];
36+
37+
foreach ($this->serviceMap->getServices() as $service) {
38+
$serviceClass = $service->getClass();
39+
40+
// todo handle abstract/parent services somehow?
41+
if (is_null($serviceClass)) {
42+
continue;
43+
}
44+
45+
foreach ($service->getTags() as $tag) {
46+
// todo could there be more tags with the same name for the same service?
47+
// todo check if message handler tag name is constant or configurable
48+
if ($tag->getName() !== 'messenger.message_handler') {
49+
continue;
50+
}
51+
52+
$tagAttributes = $tag->getAttributes();
53+
$reflectionClass = $this->reflectionProvider->getClass($serviceClass);
54+
55+
if (isset($tagAttributes['handles'])) {
56+
$handles = isset($tag['method']) ? [$tag['handles'] => $tag['method']] : [$tag['handles']];
57+
} else {
58+
$handles = $this->guessHandledMessages($reflectionClass);
59+
}
60+
61+
foreach ($handles as $messageClassName => $options) {
62+
if (is_int($messageClassName) && is_string($options)) {
63+
$messageClassName = $options;
64+
$options = [];
65+
}
66+
67+
$options['method'] = $options['method'] ?? '__invoke';
68+
69+
$methodReflection = $reflectionClass->getNativeMethod($options['method']);
70+
$variant = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
71+
72+
$returnTypesMap[$messageClassName][] = $variant->getReturnType();
73+
}
74+
}
75+
}
76+
77+
$messages = [];
78+
foreach ($returnTypesMap as $messageClassName => $returnTypes) {
79+
$messages[] = new Message($messageClassName, $returnTypes);
80+
}
81+
82+
return new MessageMap($messages);
83+
}
84+
85+
private function guessHandledMessages(ClassReflection $reflectionClass): iterable
86+
{
87+
if ($reflectionClass->implementsInterface(MessageSubscriberInterface::class)) {
88+
// todo handle different return formats
89+
return $reflectionClass->getName()::getHandledMessages();
90+
}
91+
92+
// todo handle if doesn't exists
93+
$methodReflection = $reflectionClass->getNativeMethod('__invoke');
94+
95+
$variant = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
96+
$parameters = $variant->getParameters();
97+
98+
if (count($parameters) !== 1) {
99+
// todo handle error
100+
throw new RuntimeException('invalid handler');
101+
}
102+
103+
$type = $parameters[0]->getType();
104+
105+
if ($type instanceof UnionType) {
106+
$types = [];
107+
foreach ($type->getTypes() as $type) {
108+
if (!$type instanceof ObjectType) {
109+
// todo handle error
110+
throw new RuntimeException('invalid handler');
111+
}
112+
113+
$types[] = $type->getClassName();
114+
}
115+
116+
if ($types) {
117+
return $types;
118+
}
119+
120+
// todo handle error
121+
throw new RuntimeException('invalid handler');
122+
}
123+
124+
if (!$type instanceof ObjectType) {
125+
throw new RuntimeException('invalid handler');
126+
}
127+
128+
return [$type->getClassName()];
129+
}
130+
124131
}

0 commit comments

Comments
 (0)