Skip to content
This repository has been archived by the owner on Jun 26, 2018. It is now read-only.

Commit

Permalink
Merge 6f031e4 into e6454d1
Browse files Browse the repository at this point in the history
  • Loading branch information
mzk committed Dec 29, 2017
2 parents e6454d1 + 6f031e4 commit fc1f487
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 10 deletions.
13 changes: 8 additions & 5 deletions src/Rules/ContainerInterfacePrivateServiceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
namespace Lookyman\PHPStan\Symfony\Rules;

use Lookyman\PHPStan\Symfony\ServiceMap;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Type\ObjectType;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ThisType;

final class ContainerInterfacePrivateServiceRule implements Rule
{
Expand All @@ -34,8 +35,10 @@ public function processNode(Node $node, Scope $scope): array
{
if ($node instanceof MethodCall && $node->name === 'get') {
$type = $scope->getType($node->var);
if ($type instanceof ObjectType
&& \in_array($type->getClassName(), ['Symfony\Component\DependencyInjection\ContainerInterface', 'Symfony\Bundle\FrameworkBundle\Controller\Controller'], \true)
$baseController = new ObjectType('Symfony\Bundle\FrameworkBundle\Controller\Controller');
$isInstanceOfController = $type instanceof ThisType && $baseController->isSuperTypeOf($type)->yes();
$isContainerInterface = $type instanceof ObjectType && $type->getClassName() === 'Symfony\Component\DependencyInjection\ContainerInterface';
if (($isContainerInterface || $isInstanceOfController)
&& isset($node->args[0])
&& $node->args[0] instanceof Arg
) {
Expand Down
13 changes: 8 additions & 5 deletions src/Rules/ContainerInterfaceUnknownServiceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
namespace Lookyman\PHPStan\Symfony\Rules;

use Lookyman\PHPStan\Symfony\ServiceMap;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Type\ObjectType;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ThisType;

final class ContainerInterfaceUnknownServiceRule implements Rule
{
Expand All @@ -34,8 +35,10 @@ public function processNode(Node $node, Scope $scope): array
{
if ($node instanceof MethodCall && $node->name === 'get') {
$type = $scope->getType($node->var);
if ($type instanceof ObjectType
&& \in_array($type->getClassName(), ['Symfony\Component\DependencyInjection\ContainerInterface', 'Symfony\Bundle\FrameworkBundle\Controller\Controller'], \true)
$baseController = new ObjectType('Symfony\Bundle\FrameworkBundle\Controller\Controller');
$isInstanceOfController = $type instanceof ThisType && $baseController->isSuperTypeOf($type)->yes();
$isContainerInterface = $type instanceof ObjectType && $type->getClassName() === 'Symfony\Component\DependencyInjection\ContainerInterface';
if (($isInstanceOfController || $isContainerInterface)
&& isset($node->args[0])
&& $node->args[0] instanceof Arg
) {
Expand Down
27 changes: 27 additions & 0 deletions tests/Rules/ContainerInterfacePrivateServiceRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);

namespace Lookyman\PHPStan\Symfony\Rules;

use Lookyman\PHPStan\Symfony\ServiceMap;
use PHPStan\Rules\Rule;

class ContainerInterfacePrivateServiceRuleTest extends \PHPStan\Testing\RuleTestCase
{

protected function getRule(): Rule
{
$serviceMap = new ServiceMap(__DIR__ . '/../container.xml');

return new ContainerInterfacePrivateServiceRule($serviceMap);
}

public function testGetPrivateService()
{
$this->analyse([__DIR__ . '/data/ExampleController.php'], [
[
'Service "private" is private.',
14,
],
]);
}
}
27 changes: 27 additions & 0 deletions tests/Rules/ContainerInterfaceUnknownServiceRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);

namespace Lookyman\PHPStan\Symfony\Rules;

use Lookyman\PHPStan\Symfony\ServiceMap;
use PHPStan\Rules\Rule;

class ContainerInterfaceUnknownServiceRuleTest extends \PHPStan\Testing\RuleTestCase
{

protected function getRule(): Rule
{
$serviceMap = new ServiceMap(__DIR__ . '/../container.xml');

return new ContainerInterfaceUnknownServiceRule($serviceMap);
}

public function testGetUnknownService()
{
$this->analyse([__DIR__ . '/data/ExampleController.php'], [
[
'Service "service.not.found" is not registered in the container.',
20,
],
]);
}
}
10 changes: 10 additions & 0 deletions tests/Rules/data/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types = 1);

namespace Symfony\Bundle\FrameworkBundle\Controller;

abstract class Controller
{

}
23 changes: 23 additions & 0 deletions tests/Rules/data/ExampleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types = 1);

namespace Lookyman\PHPStan\Symfony\Rules\data;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

include __DIR__ . '/Controller.php';

class ExampleController extends Controller
{

public function getPrivateServiceAction()
{
$service = $this->get('private');
$service->noMethod();
}

public function getUnknownService()
{
$service = $this->get('service.not.found');
$service->noMethod();
}
}

0 comments on commit fc1f487

Please sign in to comment.