Skip to content

Commit

Permalink
PropertyNotSetInConstructor error about $context is not raised in Con…
Browse files Browse the repository at this point in the history
…straintValidator (#63)
  • Loading branch information
seferov committed Jul 30, 2020
1 parent d13b6aa commit 750e7b9
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -20,6 +20,7 @@
"phpunit/phpunit": "~7.5",
"symfony/console": "*",
"symfony/messenger": "^4.2 || ^5.0",
"symfony/validator": "*",
"weirdan/codeception-psalm-module": "~0.8"
},
"autoload": {
Expand Down
2 changes: 0 additions & 2 deletions src/Handler/ConsoleHandler.php
Expand Up @@ -111,7 +111,6 @@ public static function afterMethodCallAnalysis(

/**
* @param Arg[] $args
* @param StatementsSource $statements_source
*/
private static function analyseArgument(array $args, StatementsSource $statements_source): void
{
Expand Down Expand Up @@ -151,7 +150,6 @@ private static function analyseArgument(array $args, StatementsSource $statement

/**
* @param Arg[] $args
* @param StatementsSource $statements_source
*/
private static function analyseOption(array $args, StatementsSource $statements_source): void
{
Expand Down
12 changes: 8 additions & 4 deletions src/Plugin.php
Expand Up @@ -22,17 +22,21 @@ class Plugin implements PluginEntryPointInterface
/**
* @return string[]
*/
protected function getCommonStubs(): array {
return glob(__DIR__ . '/Stubs/common/*.stubphp') ?: [];
protected function getCommonStubs(): array
{
return glob(__DIR__.'/Stubs/common/*.stubphp') ?: [];
}

/**
* @param int $majorVersion symfony major version
*
* @return string[]
*/
protected function getStubsForMajorVersion(int $majorVersion): array {
protected function getStubsForMajorVersion(int $majorVersion): array
{
$version = (string) $majorVersion;
return glob(__DIR__ . '/Stubs/' . $version . '/*.stubphp') ?: [];

return glob(__DIR__.'/Stubs/'.$version.'/*.stubphp') ?: [];
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/Stubs/common/ConstraintValidator.stubphp
@@ -0,0 +1,14 @@
<?php

namespace Symfony\Component\Validator;

use Symfony\Component\Validator\Context\ExecutionContextInterface;

abstract class ConstraintValidator implements ConstraintValidatorInterface
{
/**
* @var ExecutionContextInterface
* @psalm-suppress PropertyNotSetInConstructor
*/
protected $context;
}
43 changes: 43 additions & 0 deletions tests/acceptance/acceptance/ConstraintValidator.feature
@@ -0,0 +1,43 @@
@symfony-common
Feature: ConstraintValidator

Background:
Given I have the following config
"""
<?xml version="1.0"?>
<psalm errorLevel="1">
<projectFiles>
<directory name="."/>
<ignoreFiles> <directory name="../../vendor"/> </ignoreFiles>
</projectFiles>
<plugins>
<pluginClass class="Psalm\SymfonyPsalmPlugin\Plugin"/>
</plugins>
</psalm>
"""

Scenario: PropertyNotSetInConstructor error about $context is not raised
Given I have the following code
"""
<?php
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class CustomValidator extends ConstraintValidator
{
public function __construct() {}
public function validate($value, Constraint $constraint): void
{
if ($value) {
$this->context
->buildViolation('foo')
->atPath('foo')
->addViolation();
}
}
}
"""
When I run Psalm
Then I see no errors

0 comments on commit 750e7b9

Please sign in to comment.