Skip to content

Commit 2ee2ae1

Browse files
authored
Fix & improve tests (#342)
1 parent af23a49 commit 2ee2ae1

16 files changed

+126
-242
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
"vimeo/psalm": "^5.24"
1717
},
1818
"require-dev": {
19-
"symfony/form": "^5.0 || ^6.0 || ^7.0",
2019
"doctrine/annotations": "^1.8|^2",
2120
"doctrine/orm": "^2.9",
2221
"phpunit/phpunit": "~7.5 || ~9.5",
2322
"symfony/cache-contracts": "^1.0 || ^2.0",
2423
"symfony/console": "*",
24+
"symfony/form": "^5.0 || ^6.0 || ^7.0",
2525
"symfony/messenger": "^5.0 || ^6.0 || ^7.0",
26-
"symfony/security-guard": "*",
26+
"symfony/security-core": "*",
2727
"symfony/serializer": "^5.0 || ^6.0 || ^7.0",
2828
"symfony/validator": "*",
2929
"twig/twig": "^2.10 || ^3.0",

src/Plugin.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030
class Plugin implements PluginEntryPointInterface
3131
{
32-
public function __invoke(RegistrationInterface $api, ?\SimpleXMLElement $config = null): void
32+
public function __invoke(RegistrationInterface $registration, ?\SimpleXMLElement $config = null): void
3333
{
3434
require_once __DIR__.'/Handler/HeaderBagHandler.php';
3535
require_once __DIR__.'/Handler/ContainerHandler.php';
@@ -39,13 +39,13 @@ public function __invoke(RegistrationInterface $api, ?\SimpleXMLElement $config
3939
require_once __DIR__.'/Handler/DoctrineQueryBuilderHandler.php';
4040
require_once __DIR__.'/Provider/FormGetErrorsReturnTypeProvider.php';
4141

42-
$api->registerHooksFromClass(HeaderBagHandler::class);
43-
$api->registerHooksFromClass(ConsoleHandler::class);
44-
$api->registerHooksFromClass(ContainerDependencyHandler::class);
45-
$api->registerHooksFromClass(RequiredSetterHandler::class);
42+
$registration->registerHooksFromClass(HeaderBagHandler::class);
43+
$registration->registerHooksFromClass(ConsoleHandler::class);
44+
$registration->registerHooksFromClass(ContainerDependencyHandler::class);
45+
$registration->registerHooksFromClass(RequiredSetterHandler::class);
4646

4747
if (class_exists(\Doctrine\ORM\QueryBuilder::class)) {
48-
$api->registerHooksFromClass(DoctrineQueryBuilderHandler::class);
48+
$registration->registerHooksFromClass(DoctrineQueryBuilderHandler::class);
4949
}
5050

5151
if (class_exists(AnnotationRegistry::class)) {
@@ -54,10 +54,10 @@ public function __invoke(RegistrationInterface $api, ?\SimpleXMLElement $config
5454
/** @psalm-suppress DeprecatedMethod */
5555
AnnotationRegistry::registerLoader('class_exists');
5656
}
57-
$api->registerHooksFromClass(DoctrineRepositoryHandler::class);
57+
$registration->registerHooksFromClass(DoctrineRepositoryHandler::class);
5858

5959
require_once __DIR__.'/Handler/AnnotationHandler.php';
60-
$api->registerHooksFromClass(AnnotationHandler::class);
60+
$registration->registerHooksFromClass(AnnotationHandler::class);
6161
}
6262

6363
if (isset($config->containerXml)) {
@@ -83,14 +83,14 @@ public function __invoke(RegistrationInterface $api, ?\SimpleXMLElement $config
8383

8484
require_once __DIR__.'/Handler/ParameterBagHandler.php';
8585
ParameterBagHandler::init($containerMeta);
86-
$api->registerHooksFromClass(ParameterBagHandler::class);
86+
$registration->registerHooksFromClass(ParameterBagHandler::class);
8787
}
8888

89-
$api->registerHooksFromClass(ContainerHandler::class);
89+
$registration->registerHooksFromClass(ContainerHandler::class);
9090

91-
$this->addStubs($api, __DIR__.'/Stubs/common');
92-
$this->addStubs($api, __DIR__.'/Stubs/'.Kernel::MAJOR_VERSION);
93-
$this->addStubs($api, __DIR__.'/Stubs/php');
91+
$this->addStubs($registration, __DIR__.'/Stubs/common');
92+
$this->addStubs($registration, __DIR__.'/Stubs/'.Kernel::MAJOR_VERSION);
93+
$this->addStubs($registration, __DIR__.'/Stubs/php');
9494

9595
if (isset($config->twigCachePath)) {
9696
$twig_cache_path = getcwd().DIRECTORY_SEPARATOR.ltrim((string) $config->twigCachePath, DIRECTORY_SEPARATOR);
@@ -99,15 +99,15 @@ public function __invoke(RegistrationInterface $api, ?\SimpleXMLElement $config
9999
}
100100

101101
require_once __DIR__.'/Twig/CachedTemplatesTainter.php';
102-
$api->registerHooksFromClass(CachedTemplatesTainter::class);
102+
$registration->registerHooksFromClass(CachedTemplatesTainter::class);
103103

104104
require_once __DIR__.'/Twig/CachedTemplatesMapping.php';
105-
$api->registerHooksFromClass(CachedTemplatesMapping::class);
105+
$registration->registerHooksFromClass(CachedTemplatesMapping::class);
106106
CachedTemplatesMapping::setCachePath($twig_cache_path);
107107
}
108108

109109
require_once __DIR__.'/Twig/AnalyzedTemplatesTainter.php';
110-
$api->registerHooksFromClass(AnalyzedTemplatesTainter::class);
110+
$registration->registerHooksFromClass(AnalyzedTemplatesTainter::class);
111111

112112
if (isset($config->twigRootPath)) {
113113
$twig_root_path = trim((string) $config->twigRootPath, DIRECTORY_SEPARATOR);
@@ -119,20 +119,20 @@ public function __invoke(RegistrationInterface $api, ?\SimpleXMLElement $config
119119
TemplateFileAnalyzer::setTemplateRootPath($twig_root_path);
120120
}
121121

122-
$api->registerHooksFromClass(FormGetErrorsReturnTypeProvider::class);
122+
$registration->registerHooksFromClass(FormGetErrorsReturnTypeProvider::class);
123123
}
124124

125-
private function addStubs(RegistrationInterface $api, string $path): void
125+
private function addStubs(RegistrationInterface $registration, string $path): void
126126
{
127127
if (!is_dir($path)) {
128-
// e.g. looking for stubs for version 3, but there aren't any at time of writing, so don't try and load them.
128+
// e.g., looking for stubs for version 3, but there aren't any at time of writing, so don't try and load them.
129129
return;
130130
}
131131

132132
$a = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
133133
foreach ($a as $file) {
134134
if (!$file->isDir()) {
135-
$api->addStubFile($file->getPathname());
135+
$registration->addStubFile($file->getPathname());
136136
}
137137
}
138138
}

src/Stubs/common/Component/PropertyAccess/PropertyAccessorInterface.stubphp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,29 @@ interface PropertyAccessorInterface
1111
* @psalm-param mixed $value
1212
* @psalm-param-out T $objectOrArray
1313
*/
14-
public function setValue(&$objectOrArray, $propertyPath, $value);
14+
public function setValue(object|array &$objectOrArray, string|PropertyPathInterface $propertyPath, mixed $value);
1515

1616
/**
1717
* @param object|array $objectOrArray
1818
* @param string|PropertyPathInterface $propertyPath
1919
*
2020
* @return mixed
2121
*/
22-
public function getValue($objectOrArray, $propertyPath);
22+
public function getValue(object|array $objectOrArray, string|PropertyPathInterface $propertyPath): mixed;
2323

2424
/**
2525
* @param object|array $objectOrArray
2626
* @param string|PropertyPathInterface $propertyPath
2727
*
2828
* @return bool
2929
*/
30-
public function isWritable($objectOrArray, $propertyPath);
30+
public function isWritable(object|array $objectOrArray, string|PropertyPathInterface $propertyPath): bool;
3131

3232
/**
3333
* @param object|array $objectOrArray
3434
* @param string|PropertyPathInterface $propertyPath
3535
*
3636
* @return bool
3737
*/
38-
public function isReadable($objectOrArray, $propertyPath);
38+
public function isReadable(object|array $objectOrArray, string|PropertyPathInterface $propertyPath): bool;
3939
}

src/Stubs/common/Component/Security/Core/Authorization/Voter/Voter.stubphp

Lines changed: 0 additions & 42 deletions
This file was deleted.

tests/acceptance/acceptance/ContainerService.feature

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,26 @@ Feature: Container service
44

55
Background:
66
Given I have Symfony plugin enabled
7-
8-
Scenario: Asserting psalm recognizes return type of service got via 'ContainerInterface::get()'
9-
Given I have the following code
7+
And I have the following code preamble
108
"""
119
<?php
10+
11+
use \Symfony\Component\DependencyInjection\ContainerInterface;
12+
1213
class SomeService
1314
{
1415
public function do(): void {}
1516
}
17+
"""
1618

17-
class SomeController
19+
Scenario: Asserting psalm recognizes return type of service got via 'ContainerInterface::get()'
20+
Given I have the following code
21+
"""
22+
class App
1823
{
19-
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
20-
21-
public function index(): void
24+
public function __invoke(ContainerInterface $container): void
2225
{
23-
$this->container->get(SomeService::class)->do();
26+
$container->get(SomeService::class)->do();
2427
}
2528
}
2629
"""
@@ -30,19 +33,11 @@ Feature: Container service
3033
Scenario: Asserting psalm recognizes return type of service got via 'ContainerInterface::get()'.
3134
Given I have the following code
3235
"""
33-
<?php
34-
class SomeService
35-
{
36-
public function do(): void {}
37-
}
38-
39-
class SomeController
36+
class App
4037
{
41-
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
42-
43-
public function index(): void
38+
public function __invoke(ContainerInterface $container): void
4439
{
45-
$this->container->get(SomeService::class)->nosuchmethod();
40+
$container->get(SomeService::class)->nosuchmethod();
4641
}
4742
}
4843
"""
@@ -55,18 +50,16 @@ Feature: Container service
5550
Scenario: Container get(self::class) should not crash
5651
Given I have the following code
5752
"""
58-
<?php
59-
class SomeController
53+
class App
6054
{
61-
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
62-
63-
public function index()
55+
public function index(ContainerInterface $container): void
6456
{
65-
$this->container->get(self::class)->index();
57+
$container->get(self::class)->index();
6658
}
6759
}
6860
"""
6961
When I run Psalm
7062
Then I see these errors
71-
| Type | Message |
72-
| MissingReturnType | Method SomeController::index does not have a return type, expecting void |
63+
| Type | Message |
64+
| MixedMethodCall | Cannot determine the type of the object on the left hand side of this expression |
65+
And I see no other errors

tests/acceptance/acceptance/ContainerXml.feature

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@ Feature: Container XML config
77
"""
88
<containerXml>../../tests/acceptance/container.xml</containerXml>
99
"""
10+
And I have the following code preamble
11+
"""
12+
<?php
13+
14+
use \Symfony\Component\DependencyInjection\ContainerInterface;
15+
use \Symfony\Component\HttpKernel\HttpKernelInterface;
16+
"""
1017

1118
Scenario: Asserting psalm recognizes return type of service got via 'ContainerInterface::get() using service ID'
1219
Given I have the following code
1320
"""
14-
<?php
15-
16-
class SomeController
21+
class App
1722
{
18-
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
19-
20-
public function index(): bool
23+
public function __invoke(ContainerInterface $container): bool
2124
{
22-
return $this->container->get('service_container')->has('lorem');
25+
return $container->get('service_container')->has('lorem');
2326
}
2427
}
2528
"""
@@ -29,15 +32,11 @@ Feature: Container XML config
2932
Scenario: Psalm emits when service ID not found in container'
3033
Given I have the following code
3134
"""
32-
<?php
33-
34-
class SomeController
35+
class App
3536
{
36-
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
37-
38-
public function index(): void
37+
public function __invoke(ContainerInterface $container): void
3938
{
40-
$this->container->get('not_a_service')->has('lorem');
39+
$container->get('not_a_service')->has('lorem');
4140
}
4241
}
4342
"""
@@ -49,18 +48,12 @@ Feature: Container XML config
4948
Scenario: Using service both via alias and class const
5049
Given I have the following code
5150
"""
52-
<?php
53-
54-
use Symfony\Component\HttpKernel\HttpKernelInterface;
55-
56-
class SomeController
51+
class App
5752
{
58-
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
59-
60-
public function index(): void
53+
public function __invoke(ContainerInterface $container): void
6154
{
62-
$this->container->get('http_kernel');
63-
$this->container->get(HttpKernelInterface::class);
55+
$container->get('http_kernel');
56+
$container->get(HttpKernelInterface::class);
6457
}
6558
}
6659
"""
@@ -70,15 +63,11 @@ Feature: Container XML config
7063
Scenario: Using private service
7164
Given I have the following code
7265
"""
73-
<?php
74-
75-
class SomeController
66+
class App
7667
{
77-
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
78-
79-
public function index(): void
68+
public function __invoke(ContainerInterface $container): void
8069
{
81-
$this->container->get('private_service');
70+
$container->get('private_service');
8271
}
8372
}
8473
"""

0 commit comments

Comments
 (0)