mirror of
https://github.com/danog/psalm-plugin-symfony.git
synced 2025-01-22 13:01:49 +01:00
naming convention for parameters (#52)
This commit is contained in:
parent
ec7545a086
commit
fb2eb766e4
@ -18,7 +18,7 @@ to use InputArgument and InputOption constants as a part of best practise.
|
|||||||
- Fixes `PossiblyInvalidArgument` for `Symfony\Component\HttpFoundation\Request::getContent`.
|
- Fixes `PossiblyInvalidArgument` for `Symfony\Component\HttpFoundation\Request::getContent`.
|
||||||
The plugin calculates real return type by checking the given argument and marks return type as either string or resource.
|
The plugin calculates real return type by checking the given argument and marks return type as either string or resource.
|
||||||
- Detect return type of `Symfony\Component\HttpFoundation\HeaderBag::get` (by checking default value and third argument for < Symfony 4.4)
|
- Detect return type of `Symfony\Component\HttpFoundation\HeaderBag::get` (by checking default value and third argument for < Symfony 4.4)
|
||||||
- Detects service [naming convention](https://symfony.com/doc/current/contributing/code/standards.html#naming-conventions) violations
|
- Detects service and parameter [naming convention](https://symfony.com/doc/current/contributing/code/standards.html#naming-conventions) violations
|
||||||
- Complains when `Container` is injected to a service. Use dependency-injection.
|
- Complains when `Container` is injected to a service. Use dependency-injection.
|
||||||
|
|
||||||
### Configuration
|
### Configuration
|
||||||
|
@ -55,7 +55,17 @@ class ContainerHandler implements AfterMethodCallAnalysisInterface, AfterClassLi
|
|||||||
array &$file_replacements = [],
|
array &$file_replacements = [],
|
||||||
Union &$return_type_candidate = null
|
Union &$return_type_candidate = null
|
||||||
) {
|
) {
|
||||||
if (!ContainerHandler::isContainerGet($declaring_method_id)) {
|
if (!self::isContainerMethod($declaring_method_id, 'get')) {
|
||||||
|
if (self::isContainerMethod($declaring_method_id, 'getparameter')) {
|
||||||
|
$argument = $expr->args[0]->value;
|
||||||
|
if ($argument instanceof String_ && !self::followsNamingConvention($argument->value)) {
|
||||||
|
IssueBuffer::accepts(
|
||||||
|
new NamingConventionViolation(new CodeLocation($statements_source, $argument)),
|
||||||
|
$statements_source->getSuppressedIssues()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,7 +90,7 @@ class ContainerHandler implements AfterMethodCallAnalysisInterface, AfterClassLi
|
|||||||
|
|
||||||
$service = self::$containerMeta->get($serviceId);
|
$service = self::$containerMeta->get($serviceId);
|
||||||
if ($service) {
|
if ($service) {
|
||||||
if (!self::followsConvention($serviceId) && !class_exists($service->getClassName())) {
|
if (!self::followsNamingConvention($serviceId) && !class_exists($service->getClassName())) {
|
||||||
IssueBuffer::accepts(
|
IssueBuffer::accepts(
|
||||||
new NamingConventionViolation(new CodeLocation($statements_source, $expr->args[0]->value)),
|
new NamingConventionViolation(new CodeLocation($statements_source, $expr->args[0]->value)),
|
||||||
$statements_source->getSuppressedIssues()
|
$statements_source->getSuppressedIssues()
|
||||||
@ -130,13 +140,13 @@ class ContainerHandler implements AfterMethodCallAnalysisInterface, AfterClassLi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function isContainerGet(string $declaring_method_id): bool
|
private static function isContainerMethod(string $declaringMethodId, string $methodName): bool
|
||||||
{
|
{
|
||||||
return in_array(
|
return in_array(
|
||||||
$declaring_method_id,
|
$declaringMethodId,
|
||||||
array_map(
|
array_map(
|
||||||
function ($c) {
|
function ($c) use ($methodName) {
|
||||||
return $c.'::get';
|
return $c.'::'.$methodName;
|
||||||
},
|
},
|
||||||
self::GET_CLASSLIKES
|
self::GET_CLASSLIKES
|
||||||
),
|
),
|
||||||
@ -147,7 +157,7 @@ class ContainerHandler implements AfterMethodCallAnalysisInterface, AfterClassLi
|
|||||||
/**
|
/**
|
||||||
* @see https://symfony.com/doc/current/contributing/code/standards.html#naming-conventions
|
* @see https://symfony.com/doc/current/contributing/code/standards.html#naming-conventions
|
||||||
*/
|
*/
|
||||||
private static function followsConvention(string $name): bool
|
private static function followsNamingConvention(string $name): bool
|
||||||
{
|
{
|
||||||
return !preg_match('/[A-Z]/', $name);
|
return !preg_match('/[A-Z]/', $name);
|
||||||
}
|
}
|
||||||
|
@ -54,6 +54,45 @@ Feature: Naming conventions
|
|||||||
"""
|
"""
|
||||||
When I run Psalm
|
When I run Psalm
|
||||||
Then I see these errors
|
Then I see these errors
|
||||||
| Type | Message |
|
| Type | Message |
|
||||||
|
| NamingConventionViolation | Use snake_case for configuration parameter and service names |
|
||||||
|
And I see no other errors
|
||||||
|
|
||||||
|
Scenario: No naming convention violation for parameter
|
||||||
|
Given I have the following code
|
||||||
|
"""
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class SomeController
|
||||||
|
{
|
||||||
|
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
|
||||||
|
|
||||||
|
public function index(): void
|
||||||
|
{
|
||||||
|
$this->container->getParameter('kernel.cache_dir');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
When I run Psalm
|
||||||
|
And I see no other errors
|
||||||
|
|
||||||
|
Scenario: Detects parameter naming convention violation
|
||||||
|
Given I have the following code
|
||||||
|
"""
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class SomeController
|
||||||
|
{
|
||||||
|
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
|
||||||
|
|
||||||
|
public function index(): void
|
||||||
|
{
|
||||||
|
$this->container->getParameter('wronglyNamedParameter');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
When I run Psalm
|
||||||
|
Then I see these errors
|
||||||
|
| Type | Message |
|
||||||
| NamingConventionViolation | Use snake_case for configuration parameter and service names |
|
| NamingConventionViolation | Use snake_case for configuration parameter and service names |
|
||||||
And I see no other errors
|
And I see no other errors
|
||||||
|
Loading…
x
Reference in New Issue
Block a user