naming convention for parameters (#52)

This commit is contained in:
Farhad Safarov 2020-06-28 09:04:40 +03:00 committed by GitHub
parent ec7545a086
commit fb2eb766e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 9 deletions

View File

@ -18,7 +18,7 @@ to use InputArgument and InputOption constants as a part of best practise.
- 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.
- 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.
### Configuration

View File

@ -55,7 +55,17 @@ class ContainerHandler implements AfterMethodCallAnalysisInterface, AfterClassLi
array &$file_replacements = [],
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;
}
@ -80,7 +90,7 @@ class ContainerHandler implements AfterMethodCallAnalysisInterface, AfterClassLi
$service = self::$containerMeta->get($serviceId);
if ($service) {
if (!self::followsConvention($serviceId) && !class_exists($service->getClassName())) {
if (!self::followsNamingConvention($serviceId) && !class_exists($service->getClassName())) {
IssueBuffer::accepts(
new NamingConventionViolation(new CodeLocation($statements_source, $expr->args[0]->value)),
$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(
$declaring_method_id,
$declaringMethodId,
array_map(
function ($c) {
return $c.'::get';
function ($c) use ($methodName) {
return $c.'::'.$methodName;
},
self::GET_CLASSLIKES
),
@ -147,7 +157,7 @@ class ContainerHandler implements AfterMethodCallAnalysisInterface, AfterClassLi
/**
* @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);
}

View File

@ -54,6 +54,45 @@ Feature: Naming conventions
"""
When I run Psalm
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 |
And I see no other errors