Skip to content

Commit

Permalink
[BUILD] Added more strict phpstan rules (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] committed Jul 31, 2019
2 parents 66cec1c + 71d5ba2 commit aaa8955
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ $config = PhpCsFixer\Config::create()
'php_unit_internal_class' => false,
'php_unit_test_class_requires_covers' => false,
'no_superfluous_phpdoc_tags' => true,
// 'static_lambda' => true,
'static_lambda' => true,
])
->setFinder($finder)
;
Expand Down
6 changes: 6 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ parameters:
ignoreErrors:
# Symfony DI
- '#Cannot call method scalarNode\(\) on Symfony\\Component\\Config\\Definition\\Builder\\NodeParentInterface\|null.#'
- "/Call to function method_exists.. with 'Symfony.+' and 'getRootNode' will always evaluate to false./"

# Symfony Contracts
- '#Method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface::dispatch\(\) invoked with 2 parameters, 1 required.#'

# PHPUnit
-
message: '#Property .*::\$.* has no typehint specified.#'
path: tests/
2 changes: 1 addition & 1 deletion src/Action/AuthErrorAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function __invoke(): Response
$event = new AuthFailedEvent();
$this->eventDispatcher->dispatch($event, Core23LastFmEvents::AUTH_ERROR);

if ($response = $event->getResponse()) {
if (null !== $response = $event->getResponse()) {
return $response;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Action/AuthSuccessAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function __invoke(): Response
$event = new AuthSuccessEvent($session);
$this->eventDispatcher->dispatch($event, Core23LastFmEvents::AUTH_SUCCESS);

if ($response = $event->getResponse()) {
if (null !== $response = $event->getResponse()) {
return $response;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Action/CheckAuthAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public function __construct(RouterInterface $router, SessionManagerInterface $se

public function __invoke(Request $request): RedirectResponse
{
$token = $request->query->get('token');
$token = (string) $request->query->get('token', '');

if (!$token) {
if ('' === $token) {
return new RedirectResponse($this->generateUrl('core23_lastfm_auth'));
}

Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder('core23_lastfm');

// Keep compatibility with symfony/config < 4.2
if (!method_exists($treeBuilder, 'getRootNode')) {
if (!method_exists(TreeBuilder::class, 'getRootNode')) {
$rootNode = $treeBuilder->root('core23_lastfm');
} else {
$rootNode = $treeBuilder->getRootNode();
Expand Down
23 changes: 6 additions & 17 deletions tests/Action/AuthErrorActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@
namespace Core23\LastFmBundle\Tests\Action;

use Core23\LastFmBundle\Action\AuthErrorAction;
use Core23\LastFmBundle\Core23LastFmEvents;
use Core23\LastFmBundle\Event\AuthFailedEvent;
use Core23\LastFmBundle\Session\SessionManagerInterface;
use Core23\LastFmBundle\Tests\EventDispatcher\TestEventDispatcher;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Twig\Environment;

final class AuthErrorActionTest extends TestCase
Expand All @@ -37,7 +34,7 @@ protected function setUp(): void
$this->twig = $this->prophesize(Environment::class);
$this->router = $this->prophesize(RouterInterface::class);
$this->sessionManager = $this->prophesize(SessionManagerInterface::class);
$this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$this->eventDispatcher = new TestEventDispatcher();
}

public function testExecute(): void
Expand All @@ -50,15 +47,11 @@ public function testExecute(): void
->shouldBeCalled()
;

$this->eventDispatcher->dispatch(Argument::type(AuthFailedEvent::class), Core23LastFmEvents::AUTH_ERROR)
->shouldBeCalled()
;

$action = new AuthErrorAction(
$this->twig->reveal(),
$this->router->reveal(),
$this->sessionManager->reveal(),
$this->eventDispatcher->reveal()
$this->eventDispatcher
);

$response = $action();
Expand All @@ -79,17 +72,13 @@ public function testExecuteWithCaughtEvent(): void

$eventResponse = new Response();

$this->eventDispatcher->dispatch(Argument::type(AuthFailedEvent::class), Core23LastFmEvents::AUTH_ERROR)
->will(function ($args) use ($eventResponse) {
$args[0]->setResponse($eventResponse);
})
;
$this->eventDispatcher->setResponse($eventResponse);

$action = new AuthErrorAction(
$this->twig->reveal(),
$this->router->reveal(),
$this->sessionManager->reveal(),
$this->eventDispatcher->reveal()
$this->eventDispatcher
);

$response = $action();
Expand All @@ -112,7 +101,7 @@ public function testExecuteWithNoAuth(): void
$this->twig->reveal(),
$this->router->reveal(),
$this->sessionManager->reveal(),
$this->eventDispatcher->reveal()
$this->eventDispatcher
);

static::assertInstanceOf(RedirectResponse::class, $action());
Expand Down
25 changes: 7 additions & 18 deletions tests/Action/AuthSuccessActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@

use Core23\LastFm\Session\SessionInterface;
use Core23\LastFmBundle\Action\AuthSuccessAction;
use Core23\LastFmBundle\Core23LastFmEvents;
use Core23\LastFmBundle\Event\AuthSuccessEvent;
use Core23\LastFmBundle\Session\SessionManagerInterface;
use Core23\LastFmBundle\Tests\EventDispatcher\TestEventDispatcher;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Twig\Environment;

final class AuthSuccessActionTest extends TestCase
Expand All @@ -38,7 +35,7 @@ protected function setUp(): void
$this->twig = $this->prophesize(Environment::class);
$this->router = $this->prophesize(RouterInterface::class);
$this->sessionManager = $this->prophesize(SessionManagerInterface::class);
$this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$this->eventDispatcher = new TestEventDispatcher();
}

public function testExecute(): void
Expand All @@ -55,10 +52,6 @@ public function testExecute(): void
->willReturn('FooUser')
;

$this->eventDispatcher->dispatch(Argument::type(AuthSuccessEvent::class), Core23LastFmEvents::AUTH_SUCCESS)
->shouldBeCalled()
;

$this->twig->render('@Core23LastFm/Auth/success.html.twig', [
'name' => 'FooUser',
])->shouldBeCalled();
Expand All @@ -67,7 +60,7 @@ public function testExecute(): void
$this->twig->reveal(),
$this->router->reveal(),
$this->sessionManager->reveal(),
$this->eventDispatcher->reveal()
$this->eventDispatcher
);

$response = $action();
Expand All @@ -92,17 +85,13 @@ public function testExecuteWithCaughtEvent(): void

$eventResponse = new Response();

$this->eventDispatcher->dispatch(Argument::type(AuthSuccessEvent::class), Core23LastFmEvents::AUTH_SUCCESS)
->will(function ($args) use ($eventResponse) {
$args[0]->setResponse($eventResponse);
})
;
$this->eventDispatcher->setResponse($eventResponse);

$action = new AuthSuccessAction(
$this->twig->reveal(),
$this->router->reveal(),
$this->sessionManager->reveal(),
$this->eventDispatcher->reveal()
$this->eventDispatcher
);

$response = $action();
Expand All @@ -125,7 +114,7 @@ public function testExecuteNoAuth(): void
$this->twig->reveal(),
$this->router->reveal(),
$this->sessionManager->reveal(),
$this->eventDispatcher->reveal()
$this->eventDispatcher
);

static::assertInstanceOf(RedirectResponse::class, $action());
Expand All @@ -149,7 +138,7 @@ public function testExecuteNoSession(): void
$this->twig->reveal(),
$this->router->reveal(),
$this->sessionManager->reveal(),
$this->eventDispatcher->reveal()
$this->eventDispatcher
);

static::assertInstanceOf(RedirectResponse::class, $action());
Expand Down
8 changes: 0 additions & 8 deletions tests/Core23LastFmBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,9 @@
use Core23\LastFmBundle\Core23LastFmBundle;
use Core23\LastFmBundle\DependencyInjection\Core23LastFmExtension;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;

final class Core23LastFmBundleTest extends TestCase
{
public function testItIsInstantiable(): void
{
$bundle = new Core23LastFmBundle();

static::assertInstanceOf(BundleInterface::class, $bundle);
}

public function testGetContainerExtension(): void
{
$bundle = new Core23LastFmBundle();
Expand Down
8 changes: 0 additions & 8 deletions tests/Event/AuthFailedEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,9 @@
use Core23\LastFmBundle\Event\AuthFailedEvent;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\EventDispatcher\Event;

final class AuthFailedEventTest extends TestCase
{
public function testCreation(): void
{
$event = new AuthFailedEvent();

static::assertInstanceOf(Event::class, $event);
}

public function testGetResponse(): void
{
$event = new AuthFailedEvent();
Expand Down
10 changes: 0 additions & 10 deletions tests/Event/AuthSuccessEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,9 @@
use Core23\LastFmBundle\Event\AuthSuccessEvent;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\EventDispatcher\Event;

final class AuthSuccessEventTest extends TestCase
{
public function testCreation(): void
{
$session = $this->prophesize(SessionInterface::class);

$event = new AuthSuccessEvent($session->reveal());

static::assertInstanceOf(Event::class, $event);
}

public function testGetUsername(): void
{
$session = $this->prophesize(SessionInterface::class);
Expand Down
32 changes: 32 additions & 0 deletions tests/EventDispatcher/TestEventDispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Core23\LastFmBundle\Tests\EventDispatcher;

use Core23\LastFmBundle\Event\AuthFailedEvent;
use Core23\LastFmBundle\Event\AuthSuccessEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

final class TestEventDispatcher implements EventDispatcherInterface
{
private $response;

public function dispatch($event)
{
if ($event instanceof AuthFailedEvent || $event instanceof AuthSuccessEvent) {
$event->setResponse($this->response);
}
}

public function setResponse(Response $response): void
{
$this->response = $response;
}
}
4 changes: 0 additions & 4 deletions tests/Session/SessionManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace Core23\LastFmBundle\Tests\Session;

use Core23\LastFm\Session\Session as LastFmSession;
use Core23\LastFm\Session\SessionInterface;
use Core23\LastFmBundle\Session\SessionManager;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Session;
Expand Down Expand Up @@ -71,8 +70,6 @@ public function testStore(): void

$manager = new SessionManager($session->reveal());
$manager->store($lastfmSession);

static::assertTrue(true);
}

public function testClear(): void
Expand All @@ -97,7 +94,6 @@ public function testGetSession(): void

$manager = new SessionManager($session->reveal());

/** @var SessionInterface $lastfmSession */
$lastfmSession = $manager->getSession();

static::assertNotNull($lastfmSession);
Expand Down
5 changes: 4 additions & 1 deletion vendor-bin/phpstan/composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{
"require": {
"ekino/phpstan-banned-code": "^0.1",
"jangregor/phpstan-prophecy": "^0.4",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "^0.11",
"phpstan/phpstan-phpunit": "^0.11"
"phpstan/phpstan-phpunit": "^0.11",
"phpstan/phpstan-strict-rules": "^0.11",
"phpstan/phpstan-symfony": "^0.11"
},
"conflict": {
"phpunit/phpunit": ">=8.0"
Expand Down

0 comments on commit aaa8955

Please sign in to comment.