Skip to content

Commit

Permalink
Merge pull request #12 from jslmorrison/psalm-integration
Browse files Browse the repository at this point in the history
Psalm integration
  • Loading branch information
weierophinney committed Apr 7, 2022
2 parents 4ba549d + 3c1c3fb commit daece00
Show file tree
Hide file tree
Showing 11 changed files with 2,754 additions and 971 deletions.
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
},
"require-dev": {
"laminas/laminas-coding-standard": "~2.3.0",
"phpunit/phpunit": "^9.5.11"
"phpunit/phpunit": "^9.5.11",
"psalm/plugin-phpunit": "^0.16.1",
"vimeo/psalm": "^4.8"
},
"autoload": {
"psr-4": {
Expand All @@ -57,7 +59,8 @@
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"test": "phpunit --colors=always",
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml"
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml",
"static-analysis": "psalm --shepherd --stats"
},
"conflict": {
"zendframework/zend-expressive-authentication-zendauthentication": "*"
Expand Down
3,553 changes: 2,621 additions & 932 deletions composer.lock

Large diffs are not rendered by default.

File renamed without changes.
42 changes: 42 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.22.0@fc2c6ab4d5fa5d644d8617089f012f3bb84b8703">
<file src="src/LaminasAuthentication.php">
<MixedArgument occurrences="1">
<code>$this-&gt;config['redirect']</code>
</MixedArgument>
<MixedInferredReturnType occurrences="1">
<code>UserInterface</code>
</MixedInferredReturnType>
<MixedReturnStatement occurrences="1">
<code>$userFactory($identity, $roles, $details)</code>
</MixedReturnStatement>
<PossiblyNullReference occurrences="2">
<code>setCredential</code>
<code>setIdentity</code>
</PossiblyNullReference>
</file>
<file src="src/LaminasAuthenticationFactory.php">
<MixedArgument occurrences="2">
<code>$auth</code>
<code>$container-&gt;get(UserInterface::class)</code>
</MixedArgument>
<MixedAssignment occurrences="1">
<code>$auth</code>
</MixedAssignment>
<PossiblyInvalidArgument occurrences="1">
<code>$config</code>
</PossiblyInvalidArgument>
</file>
<file src="test/LaminasAuthenticationTest.php">
<MixedArgumentTypeCoercion occurrences="8">
<code>$this-&gt;responseFactory</code>
<code>$this-&gt;responseFactory</code>
<code>$this-&gt;responseFactory</code>
<code>$this-&gt;responseFactory</code>
<code>$this-&gt;responseFactory</code>
<code>$this-&gt;responseFactory</code>
<code>$this-&gt;responseFactory</code>
<code>$this-&gt;responseFactory</code>
</MixedArgumentTypeCoercion>
</file>
</files>
56 changes: 56 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorBaseline="psalm-baseline.xml"
>
<projectFiles>
<directory name="src"/>
<directory name="test"/>
<ignoreFiles>
<directory name="vendor"/>
</ignoreFiles>
</projectFiles>

<issueHandlers>
<DeprecatedClass>
<errorLevel type="suppress">
<referencedClass name="Mezzio\Authentication\LaminasAuthentication\Response\CallableResponseFactoryDecorator"/>
</errorLevel>
</DeprecatedClass>
<DeprecatedTrait>
<errorLevel type="suppress">
<file name="src/LaminasAuthenticationFactory.php"/>
</errorLevel>
</DeprecatedTrait>
<InternalMethod>
<errorLevel type="suppress">
<referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::method"/>
</errorLevel>
<errorLevel type="suppress">
<referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::willReturn"/>
</errorLevel>
<errorLevel type="suppress">
<referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::with"/>
</errorLevel>
</InternalMethod>
<UndefinedClass>
<errorLevel type="suppress">
<referencedClass name="Mezzio\Container\ResponseFactoryFactory"/>
</errorLevel>
</UndefinedClass>
<UndefinedInterfaceMethod>
<errorLevel type="suppress">
<referencedMethod name="Laminas\Authentication\Adapter\AdapterInterface::setCredential"/>
</errorLevel>
<errorLevel type="suppress">
<referencedMethod name="Laminas\Authentication\Adapter\AdapterInterface::setIdentity"/>
</errorLevel>
</UndefinedInterfaceMethod>
</issueHandlers>
<plugins>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
</plugins>
</psalm>
6 changes: 0 additions & 6 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ public function getAuthenticationConfig(): array
public function getDependencies(): array
{
return [
// Legacy Zend Framework aliases
'aliases' => [
// @codingStandardsIgnoreStart
\Zend\Expressive\Authentication\ZendAuthentication\ZendAuthentication::class => LaminasAuthentication::class,
// @codingStandardsIgnoreEnd
],
'factories' => [
LaminasAuthentication::class => LaminasAuthenticationFactory::class,
],
Expand Down
11 changes: 7 additions & 4 deletions src/LaminasAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Mezzio\Authentication\AuthenticationInterface;
use Mezzio\Authentication\LaminasAuthentication\Response\CallableResponseFactoryDecorator;
use Mezzio\Authentication\UserInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

Expand All @@ -22,8 +23,7 @@ class LaminasAuthentication implements AuthenticationInterface
/** @var array */
protected $config;

// phpcs:disable SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration.InvalidFormat
/** @var (callable():ResponseInterface)|ResponseFactoryInterface */
/** @var ResponseFactoryInterface */
protected $responseFactory;

/** @var callable */
Expand Down Expand Up @@ -70,7 +70,7 @@ public function authenticate(ServerRequestInterface $request): ?UserInterface
}
return null;
}

/** @var UserInterface */
return ($this->userFactory)($this->auth->getIdentity());
}

Expand All @@ -86,8 +86,10 @@ public function unauthorizedResponse(ServerRequestInterface $request): ResponseI

private function initiateAuthentication(ServerRequestInterface $request): ?UserInterface
{
$params = $request->getParsedBody();
$params = $request->getParsedBody();
/** @var string */
$username = $this->config['username'] ?? 'username';
/** @var string */
$password = $this->config['password'] ?? 'password';

if (! isset($params[$username]) || ! isset($params[$password])) {
Expand All @@ -102,6 +104,7 @@ private function initiateAuthentication(ServerRequestInterface $request): ?UserI
return null;
}

/** @var UserInterface*/
return ($this->userFactory)($result->getIdentity());
}
}
13 changes: 3 additions & 10 deletions src/LaminasAuthenticationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ public function __invoke(ContainerInterface $container): LaminasAuthentication
{
$auth = $container->has(AuthenticationService::class)
? $container->get(AuthenticationService::class)
: ($container->has(\Zend\Authentication\AuthenticationService::class)
? $container->get(\Zend\Authentication\AuthenticationService::class)
: null);
: null;

if (null === $auth) {
throw new Exception\InvalidConfigException(sprintf(
Expand All @@ -38,10 +36,7 @@ public function __invoke(ContainerInterface $container): LaminasAuthentication
);
}

if (
! $container->has(UserInterface::class)
&& ! $container->has(\Zend\Expressive\Authentication\UserInterface::class)
) {
if (! $container->has(UserInterface::class)) {
throw new Exception\InvalidConfigException(
'UserInterface factory service is missing for authentication'
);
Expand All @@ -51,9 +46,7 @@ public function __invoke(ContainerInterface $container): LaminasAuthentication
$auth,
$config,
$this->detectResponseFactory($container),
$container->has(UserInterface::class)
? $container->get(UserInterface::class)
: $container->get(\Zend\Expressive\Authentication\UserInterface::class)
$container->get(UserInterface::class)
);
}
}
7 changes: 5 additions & 2 deletions test/ConfigProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

class ConfigProviderTest extends TestCase
{
/** @var ConfigProvider */
private $provider;

public function setUp(): void
{
$this->provider = new ConfigProvider();
Expand All @@ -25,7 +28,7 @@ public function testInvocationReturnsArray(): array
/**
* @depends testInvocationReturnsArray
*/
public function testReturnedArrayContainsDependencies(array $config)
public function testReturnedArrayContainsDependencies(array $config): void
{
$this->assertArrayHasKey('dependencies', $config);
$this->assertInternalType('array', $config['dependencies']);
Expand All @@ -34,7 +37,7 @@ public function testReturnedArrayContainsDependencies(array $config)
/**
* @depends testInvocationReturnsArray
*/
public function testReturnedArrayContainsAuthenticationConfig(array $config)
public function testReturnedArrayContainsAuthenticationConfig(array $config): void
{
$this->assertArrayHasKey('authentication', $config);
$this->assertInternalType('array', $config['authentication']);
Expand Down
10 changes: 5 additions & 5 deletions test/LaminasAuthenticationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,24 @@ public function setUp(): void
{
$this->authService = $this->createMock(AuthenticationService::class);
$this->responsePrototype = $this->createMock(ResponseInterface::class);
$this->responseFactory = function () {
$this->responseFactory = function (): ResponseInterface {
return $this->responsePrototype;
};
$this->userPrototype = $this->createMock(UserInterface::class);
$this->userFactory = function () {
$this->userFactory = function (): UserInterface {
return $this->userPrototype;
};
}

public function testInvokeWithEmptyContainer()
public function testInvokeWithEmptyContainer(): void
{
$container = $this->createMock(ContainerInterface::class);
$factory = new LaminasAuthenticationFactory();
$this->expectException(InvalidConfigException::class);
$factory($container);
}

public function testInvokeWithContainerEmptyConfig()
public function testInvokeWithContainerEmptyConfig(): void
{
$container = $this->createMock(ContainerInterface::class);
$container
Expand All @@ -80,7 +80,7 @@ public function testInvokeWithContainerEmptyConfig()
$factory($container);
}

public function testInvokeWithContainerAndConfig()
public function testInvokeWithContainerAndConfig(): void
{
$container = $this->createMock(ContainerInterface::class);
$container
Expand Down
20 changes: 10 additions & 10 deletions test/LaminasAuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ public function setUp(): void
{
$this->request = $this->createMock(ServerRequestInterface::class);
$this->authService = $this->createMock(AuthenticationService::class);
$this->responseFactory = function () {
$this->responseFactory = function (): ResponseInterface {
return $this->createMock(ResponseInterface::class);
};
$this->userPrototype = $this->createMock(UserInterface::class);
$this->userFactory = function () {
$this->userFactory = function (): UserInterface {
return $this->userPrototype;
};
}

public function testConstructor()
public function testConstructor(): void
{
$laminasAuthentication = new LaminasAuthentication(
$this->authService,
Expand All @@ -56,7 +56,7 @@ public function testConstructor()
$this->assertInstanceOf(AuthenticationInterface::class, $laminasAuthentication);
}

public function testAuthenticateWithGetMethodAndIdentity()
public function testAuthenticateWithGetMethodAndIdentity(): void
{
$this->request->method('getMethod')->willReturn('GET');
$this->authService->method('hasIdentity')->willReturn(true);
Expand All @@ -72,7 +72,7 @@ public function testAuthenticateWithGetMethodAndIdentity()
$this->assertInstanceOf(UserInterface::class, $result);
}

public function testAuthenticateWithGetMethodAndNoIdentity()
public function testAuthenticateWithGetMethodAndNoIdentity(): void
{
$this->request->method('getMethod')->willReturn('GET');
$this->authService->method('hasIdentity')->willReturn(false);
Expand All @@ -86,7 +86,7 @@ public function testAuthenticateWithGetMethodAndNoIdentity()
$this->assertNull($laminasAuthentication->authenticate($this->request));
}

public function testAuthenticateWithPostMethodAndNoParams()
public function testAuthenticateWithPostMethodAndNoParams(): void
{
$this->request->method('getMethod')->willReturn('POST');
$this->request->method('getParsedBody')->willReturn([]);
Expand All @@ -100,7 +100,7 @@ public function testAuthenticateWithPostMethodAndNoParams()
$this->assertNull($laminasAuthentication->authenticate($this->request));
}

public function testAuthenticateWithPostMethodAndNoValidCredential()
public function testAuthenticateWithPostMethodAndNoValidCredential(): void
{
//not authenticated
$this->authService->method('hasIdentity')->willReturn(false);
Expand Down Expand Up @@ -133,7 +133,7 @@ public function testAuthenticateWithPostMethodAndNoValidCredential()
$this->assertNull($laminasAuthentication->authenticate($this->request));
}

public function testAuthenticateWithPostMethodAndValidCredential()
public function testAuthenticateWithPostMethodAndValidCredential(): void
{
//not authenticated
$this->authService->method('hasIdentity')->willReturn(false);
Expand Down Expand Up @@ -168,7 +168,7 @@ public function testAuthenticateWithPostMethodAndValidCredential()
$this->assertInstanceOf(UserInterface::class, $result);
}

public function testAuthenticateWithPostMethodAndNoValidCredentialAndAlreadyAuthenticated()
public function testAuthenticateWithPostMethodAndNoValidCredentialAndAlreadyAuthenticated(): void
{
$this->authService->method('hasIdentity')->willReturn(true);
$this->authService->method('getIdentity')->willReturn('string');
Expand Down Expand Up @@ -205,7 +205,7 @@ public function testAuthenticateWithPostMethodAndNoValidCredentialAndAlreadyAuth
$this->assertEquals('string', $identity->getIdentity());
}

public function testAuthenticateWithPostMethodAndValidCredentialAndAlreadyAuthenticated()
public function testAuthenticateWithPostMethodAndValidCredentialAndAlreadyAuthenticated(): void
{
$this->authService->method('hasIdentity')->willReturn(true);
$this->authService->method('getIdentity')->willReturn('string');
Expand Down

0 comments on commit daece00

Please sign in to comment.