Skip to content

Commit 9a80a98

Browse files
committed
Add allow Throwable option
1 parent fc3d96b commit 9a80a98

File tree

6 files changed

+314
-52
lines changed

6 files changed

+314
-52
lines changed

Plugin.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ class Plugin implements PluginEntryPointInterface
1111
/** @return void */
1212
public function __invoke(RegistrationInterface $psalm, ?SimpleXMLElement $config = null)
1313
{
14-
$psalm->addStubFile(__DIR__ . '/stubs/LoggerInterface.php');
15-
$psalm->addStubFile(__DIR__ . '/stubs/AbstractLogger.php');
16-
$psalm->addStubFile(__DIR__ . '/stubs/LoggerTrait.php');
14+
if (isset($config->throwable) && ((bool)$config->throwable === true)) {
15+
$psalm->addStubFile(__DIR__ . '/throwable-stubs/LoggerInterface.php');
16+
$psalm->addStubFile(__DIR__ . '/throwable-stubs/AbstractLogger.php');
17+
} else {
18+
$psalm->addStubFile(__DIR__ . '/stubs/LoggerInterface.php');
19+
$psalm->addStubFile(__DIR__ . '/stubs/AbstractLogger.php');
20+
}
1721
}
1822
}

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@ $ vendor/bin/psalm-plugin enable struggle-for-php/sfp-psalm-psr-log-plugin
1717

1818
use Psr\Log\LoggerInterface;
1919

20-
class Foo
20+
class FooController
2121
{
2222
/** @var LoggerInterface */
2323
private $logger;
2424

2525
public function anyAction()
2626
{
2727
try {
28+
// do something
2829
} catch (\Exception $e) {
29-
$this->logger->error('error happend.', ['exception' => $e->getMessage()]);
30+
$this->logger->error('error happened.', ['exception' => $e->getMessage()]);
3031
}
3132
}
3233
}
@@ -35,5 +36,17 @@ class Foo
3536
would be ERROR output:
3637
```php
3738
ERROR: InvalidArgument - src/Foo.php:14:45 - Argument 2 of Psr\Log\LoggerInterface::error expects array{exception?: Exception}, array{exception: string} provided
38-
$this->logger->error('error happend.', ['exception' => $e->getMessage()]);
39-
```
39+
$this->logger->error('error happened.', ['exception' => $e->getMessage()]);
40+
```
41+
42+
## Configure
43+
44+
If you want to allow `Throwable` to pass logger context's `exception`.
45+
46+
```xml
47+
<plugins>
48+
<pluginClass class="Sfp\Psalm\PsrLogPlugin\Plugin">
49+
<throwable>1</throwable>
50+
</pluginClass>
51+
</plugins>
52+
```
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
Feature: Exception
2+
3+
Background:
4+
Given I have the following config
5+
"""
6+
<?xml version="1.0"?>
7+
<psalm totallyTyped="true">
8+
<projectFiles>
9+
<directory name="."/>
10+
<ignoreFiles> <directory name="../../vendor"/> </ignoreFiles>
11+
</projectFiles>
12+
<plugins>
13+
<pluginClass class="Sfp\Psalm\PsrLogPlugin\Plugin"/>
14+
</plugins>
15+
</psalm>
16+
"""
17+
And I have the following code preamble
18+
"""
19+
<?php
20+
use Psr\Log\LoggerInterface;
21+
use Psr\Log\AbstractLogger;
22+
23+
/**
24+
* @psalm-suppress InvalidReturnType
25+
* @return LoggerInterface
26+
*/
27+
function impl_interface() {}
28+
29+
/**
30+
* @psalm-suppress InvalidReturnType
31+
* @return AbstractLogger
32+
*/
33+
function concrete_abstract() {}
34+
35+
"""
36+
37+
Scenario: `exception` key is actually an Exception Object AS per LoggerInterface
38+
Given I have the following code
39+
"""
40+
impl_interface()->emergency("message", ['exception' => 'foo']);
41+
impl_interface()->alert("message", ['exception' => 'foo']);
42+
impl_interface()->critical("message", ['exception' => 'foo']);
43+
impl_interface()->error("message", ['exception' => 'foo']);
44+
impl_interface()->warning("message", ['exception' => 'foo']);
45+
impl_interface()->notice("message", ['exception' => 'foo']);
46+
impl_interface()->info("message", ['exception' => 'foo']);
47+
impl_interface()->debug("message", ['exception' => 'foo']);
48+
impl_interface()->emergency("message", ['exception' => new Error]);
49+
impl_interface()->alert("message", ['exception' => new Error]);
50+
impl_interface()->critical("message", ['exception' => new Error]);
51+
impl_interface()->error("message", ['exception' => new Error]);
52+
impl_interface()->warning("message", ['exception' => new Error]);
53+
impl_interface()->notice("message", ['exception' => new Error]);
54+
impl_interface()->info("message", ['exception' => new Error]);
55+
impl_interface()->debug("message", ['exception' => new Error]);
56+
"""
57+
When I run Psalm
58+
Then I see these errors
59+
| Type | Message |
60+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::emergency expects array{exception?: Exception}, array{exception: string(foo)} provided |
61+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::alert expects array{exception?: Exception}, array{exception: string(foo)} provided |
62+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::critical expects array{exception?: Exception}, array{exception: string(foo)} provided |
63+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::error expects array{exception?: Exception}, array{exception: string(foo)} provided |
64+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::warning expects array{exception?: Exception}, array{exception: string(foo)} provided |
65+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::notice expects array{exception?: Exception}, array{exception: string(foo)} provided |
66+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::info expects array{exception?: Exception}, array{exception: string(foo)} provided |
67+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::debug expects array{exception?: Exception}, array{exception: string(foo)} provided |
68+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::emergency expects array{exception?: Exception}, array{exception: Error} provided |
69+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::alert expects array{exception?: Exception}, array{exception: Error} provided |
70+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::critical expects array{exception?: Exception}, array{exception: Error} provided |
71+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::error expects array{exception?: Exception}, array{exception: Error} provided |
72+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::warning expects array{exception?: Exception}, array{exception: Error} provided |
73+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::notice expects array{exception?: Exception}, array{exception: Error} provided |
74+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::info expects array{exception?: Exception}, array{exception: Error} provided |
75+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::debug expects array{exception?: Exception}, array{exception: Error} provided |
76+
And I see no other errors
77+
78+
Scenario: `exception` key is actually an Exception Object AS per AbstractLogger
79+
Given I have the following code
80+
"""
81+
concrete_abstract()->emergency("message", ['exception' => 'foo']);
82+
concrete_abstract()->alert("message", ['exception' => 'foo']);
83+
concrete_abstract()->critical("message", ['exception' => 'foo']);
84+
concrete_abstract()->error("message", ['exception' => 'foo']);
85+
concrete_abstract()->warning("message", ['exception' => 'foo']);
86+
concrete_abstract()->notice("message", ['exception' => 'foo']);
87+
concrete_abstract()->info("message", ['exception' => 'foo']);
88+
concrete_abstract()->debug("message", ['exception' => 'foo']);
89+
concrete_abstract()->emergency("message", ['exception' => new Error]);
90+
concrete_abstract()->alert("message", ['exception' => new Error]);
91+
concrete_abstract()->critical("message", ['exception' => new Error]);
92+
concrete_abstract()->error("message", ['exception' => new Error]);
93+
concrete_abstract()->warning("message", ['exception' => new Error]);
94+
concrete_abstract()->notice("message", ['exception' => new Error]);
95+
concrete_abstract()->info("message", ['exception' => new Error]);
96+
concrete_abstract()->debug("message", ['exception' => new Error]);
97+
"""
98+
When I run Psalm
99+
Then I see these errors
100+
| Type | Message |
101+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::emergency expects array{exception?: Exception}, array{exception: string(foo)} provided |
102+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::alert expects array{exception?: Exception}, array{exception: string(foo)} provided |
103+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::critical expects array{exception?: Exception}, array{exception: string(foo)} provided |
104+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::error expects array{exception?: Exception}, array{exception: string(foo)} provided |
105+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::warning expects array{exception?: Exception}, array{exception: string(foo)} provided |
106+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::notice expects array{exception?: Exception}, array{exception: string(foo)} provided |
107+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::info expects array{exception?: Exception}, array{exception: string(foo)} provided |
108+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::debug expects array{exception?: Exception}, array{exception: string(foo)} provided |
109+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::emergency expects array{exception?: Exception}, array{exception: Error} provided |
110+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::alert expects array{exception?: Exception}, array{exception: Error} provided |
111+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::critical expects array{exception?: Exception}, array{exception: Error} provided |
112+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::error expects array{exception?: Exception}, array{exception: Error} provided |
113+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::warning expects array{exception?: Exception}, array{exception: Error} provided |
114+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::notice expects array{exception?: Exception}, array{exception: Error} provided |
115+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::info expects array{exception?: Exception}, array{exception: Error} provided |
116+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::debug expects array{exception?: Exception}, array{exception: Error} provided |
117+
And I see no other errors
Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Feature: Logger
1+
Feature: Throwable
22

33
Background:
44
Given I have the following config
@@ -10,7 +10,9 @@ Feature: Logger
1010
<ignoreFiles> <directory name="../../vendor"/> </ignoreFiles>
1111
</projectFiles>
1212
<plugins>
13-
<pluginClass class="Sfp\Psalm\PsrLogPlugin\Plugin"/>
13+
<pluginClass class="Sfp\Psalm\PsrLogPlugin\Plugin">
14+
<throwable>1</throwable>
15+
</pluginClass>
1416
</plugins>
1517
</psalm>
1618
"""
@@ -19,7 +21,6 @@ Feature: Logger
1921
<?php
2022
use Psr\Log\LoggerInterface;
2123
use Psr\Log\AbstractLogger;
22-
use Psr\Log\LoggerTrait;
2324
2425
/**
2526
* @psalm-suppress InvalidReturnType
@@ -35,7 +36,7 @@ Feature: Logger
3536
3637
"""
3738

38-
Scenario: `exception` key is actually an Exception Object AS per LoggerInterface
39+
Scenario: `exception` key is actually an Throwable Object AS per LoggerInterface
3940
Given I have the following code
4041
"""
4142
impl_interface()->emergency("message", ['exception' => 'foo']);
@@ -46,21 +47,29 @@ Feature: Logger
4647
impl_interface()->notice("message", ['exception' => 'foo']);
4748
impl_interface()->info("message", ['exception' => 'foo']);
4849
impl_interface()->debug("message", ['exception' => 'foo']);
50+
impl_interface()->emergency("message", ['exception' => new Error]);
51+
impl_interface()->alert("message", ['exception' => new Error]);
52+
impl_interface()->critical("message", ['exception' => new Error]);
53+
impl_interface()->error("message", ['exception' => new Error]);
54+
impl_interface()->warning("message", ['exception' => new Error]);
55+
impl_interface()->notice("message", ['exception' => new Error]);
56+
impl_interface()->info("message", ['exception' => new Error]);
57+
impl_interface()->debug("message", ['exception' => new Error]);
4958
"""
5059
When I run Psalm
5160
Then I see these errors
5261
| Type | Message |
53-
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::emergency expects array{exception?: Exception}, array{exception: string(foo)} provided |
54-
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::alert expects array{exception?: Exception}, array{exception: string(foo)} provided |
55-
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::critical expects array{exception?: Exception}, array{exception: string(foo)} provided |
56-
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::error expects array{exception?: Exception}, array{exception: string(foo)} provided |
57-
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::warning expects array{exception?: Exception}, array{exception: string(foo)} provided |
58-
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::notice expects array{exception?: Exception}, array{exception: string(foo)} provided |
59-
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::info expects array{exception?: Exception}, array{exception: string(foo)} provided |
60-
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::debug expects array{exception?: Exception}, array{exception: string(foo)} provided |
62+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::emergency expects array{exception?: Throwable}, array{exception: string(foo)} provided |
63+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::alert expects array{exception?: Throwable}, array{exception: string(foo)} provided |
64+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::critical expects array{exception?: Throwable}, array{exception: string(foo)} provided |
65+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::error expects array{exception?: Throwable}, array{exception: string(foo)} provided |
66+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::warning expects array{exception?: Throwable}, array{exception: string(foo)} provided |
67+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::notice expects array{exception?: Throwable}, array{exception: string(foo)} provided |
68+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::info expects array{exception?: Throwable}, array{exception: string(foo)} provided |
69+
| InvalidArgument | Argument 2 of Psr\Log\LoggerInterface::debug expects array{exception?: Throwable}, array{exception: string(foo)} provided |
6170
And I see no other errors
6271

63-
Scenario: `exception` key is actually an Exception Object AS per AbstractLogger
72+
Scenario: `exception` key is actually an Throwable Object AS per AbstractLogger
6473
Given I have the following code
6574
"""
6675
concrete_abstract()->emergency("message", ['exception' => 'foo']);
@@ -71,16 +80,24 @@ Feature: Logger
7180
concrete_abstract()->notice("message", ['exception' => 'foo']);
7281
concrete_abstract()->info("message", ['exception' => 'foo']);
7382
concrete_abstract()->debug("message", ['exception' => 'foo']);
83+
concrete_abstract()->emergency("message", ['exception' => new Error]);
84+
concrete_abstract()->alert("message", ['exception' => new Error]);
85+
concrete_abstract()->critical("message", ['exception' => new Error]);
86+
concrete_abstract()->error("message", ['exception' => new Error]);
87+
concrete_abstract()->warning("message", ['exception' => new Error]);
88+
concrete_abstract()->notice("message", ['exception' => new Error]);
89+
concrete_abstract()->info("message", ['exception' => new Error]);
90+
concrete_abstract()->debug("message", ['exception' => new Error]);
7491
"""
7592
When I run Psalm
7693
Then I see these errors
7794
| Type | Message |
78-
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::emergency expects array{exception?: Exception}, array{exception: string(foo)} provided |
79-
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::alert expects array{exception?: Exception}, array{exception: string(foo)} provided |
80-
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::critical expects array{exception?: Exception}, array{exception: string(foo)} provided |
81-
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::error expects array{exception?: Exception}, array{exception: string(foo)} provided |
82-
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::warning expects array{exception?: Exception}, array{exception: string(foo)} provided |
83-
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::notice expects array{exception?: Exception}, array{exception: string(foo)} provided |
84-
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::info expects array{exception?: Exception}, array{exception: string(foo)} provided |
85-
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::debug expects array{exception?: Exception}, array{exception: string(foo)} provided |
95+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::emergency expects array{exception?: Throwable}, array{exception: string(foo)} provided |
96+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::alert expects array{exception?: Throwable}, array{exception: string(foo)} provided |
97+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::critical expects array{exception?: Throwable}, array{exception: string(foo)} provided |
98+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::error expects array{exception?: Throwable}, array{exception: string(foo)} provided |
99+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::warning expects array{exception?: Throwable}, array{exception: string(foo)} provided |
100+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::notice expects array{exception?: Throwable}, array{exception: string(foo)} provided |
101+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::info expects array{exception?: Throwable}, array{exception: string(foo)} provided |
102+
| InvalidArgument | Argument 2 of Psr\Log\AbstractLogger::debug expects array{exception?: Throwable}, array{exception: string(foo)} provided |
86103
And I see no other errors

0 commit comments

Comments
 (0)