Skip to content

Commit

Permalink
Fixed static analysis errors and set baseline for now
Browse files Browse the repository at this point in the history
  • Loading branch information
visto9259 committed Oct 30, 2024
1 parent 973f7b3 commit b208009
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 16 deletions.
40 changes: 39 additions & 1 deletion psalm.baseline.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.25.0@01a8eb06b9e9cc6cfb6a320bf9fb14331919d505">
<files psalm-version="5.26.1@d747f6500b38ac4f7dfc5edbcae6e4b637d7add0">
<file src="src/Collector/RbacCollector.php">
<DocblockTypeContradiction>
<code><![CDATA[null === $application]]></code>
</DocblockTypeContradiction>
</file>
<file src="test/Collector/RbacCollectorTest.php">
<PossiblyNullArgument>
<code><![CDATA[$collector->serialize()]]></code>
<code><![CDATA[$collector->serialize()]]></code>
</PossiblyNullArgument>
</file>
<file src="test/ConfigProviderTest.php">
<MixedArgument>
<code><![CDATA[$result['template_map']['laminas-developer-tools/toolbar/lmc-rbac']]]></code>
<code><![CDATA[$result['view_manager']['template_map']['laminas-developer-tools/toolbar/lmc-rbac']]]></code>
</MixedArgument>
<MixedArrayAccess>
<code><![CDATA[$result['template_map']['laminas-developer-tools/toolbar/lmc-rbac']]]></code>
<code><![CDATA[$result['view_manager']['template_map']]]></code>
<code><![CDATA[$result['view_manager']['template_map']['laminas-developer-tools/toolbar/lmc-rbac']]]></code>
</MixedArrayAccess>
<MixedArrayAssignment>
<code><![CDATA[$result['template_map']['laminas-developer-tools/toolbar/lmc-rbac']]]></code>
<code><![CDATA[$result['view_manager']['template_map']]]></code>
<code><![CDATA[$result['view_manager']['template_map']['laminas-developer-tools/toolbar/lmc-rbac']]]></code>
</MixedArrayAssignment>
</file>
<file src="test/Util/ServiceManagerFactory.php">
<MixedArgument>
<code><![CDATA[$config['service_manager'] ?? []]]></code>
</MixedArgument>
<PossiblyUnusedMethod>
<code><![CDATA[getServiceManager]]></code>
</PossiblyUnusedMethod>
<RiskyTruthyFalsyComparison>
<code><![CDATA[$config]]></code>
</RiskyTruthyFalsyComparison>
</file>
</files>
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
resolveFromConfigFile="true"
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"
findUnusedBaselineEntry="true"
findUnusedCode="true"
findUnusedPsalmSuppress="true"
errorBaseline="psalm.baseline.xml"
>
<projectFiles>
<directory name="src" />
Expand Down
28 changes: 18 additions & 10 deletions src/Collector/RbacCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,16 @@ public function getPriority(): int
*/
public function collect(MvcEvent $mvcEvent): void
{
if (! $application = $mvcEvent->getApplication()) {
$application = $mvcEvent->getApplication();
if (null === $application) {
return;
}

$serviceManager = $application->getServiceManager();
/** @var RoleService $roleService */
/** @var RoleService $roleService */
$roleService = $serviceManager->get(RoleService::class);
/** @var ModuleOptions $options */
/** @var ModuleOptions $options */
$options = $serviceManager->get(ModuleOptions::class);
$this->collectOptions($options);
$this->collectOptions($options);
$this->collectGuards($options->getGuards());
$this->collectIdentityRolesAndPermissions($roleService);
}
Expand All @@ -109,6 +109,7 @@ private function collectOptions(ModuleOptions $moduleOptions): void
private function collectGuards(array $guards): void
{
$this->collectedGuards = [];
/** @var array $rules */
foreach ($guards as $type => $rules) {
$this->collectedGuards[$type] = $rules;
}
Expand All @@ -126,6 +127,7 @@ private function collectIdentityRolesAndPermissions(RoleService $roleService): v
new RecursiveRoleIterator($identityRoles),
RecursiveIteratorIterator::SELF_FIRST
);
/** @var RoleInterface $role */
foreach ($iterator as $role) {
$roleName = $role->getName();
$this->collectedRoles[] = $roleName;
Expand Down Expand Up @@ -157,17 +159,19 @@ private function collectIdentityRolesAndPermissions(RoleService $roleService): v
private function collectPermissions(RoleInterface $role): void
{
if (method_exists($role, 'getPermissions')) {
/** @var array<string>|Traversable $permissions */
$permissions = $role->getPermissions();
} else {
$reflectionProperty = new ReflectionProperty($role, 'permissions');
$permissions = $reflectionProperty->getValue($role);
/** @var array<string>|Traversable $permissions */
$permissions = $reflectionProperty->getValue($role);
}

if ($permissions instanceof Traversable) {
$permissions = iterator_to_array($permissions);
}

array_walk($permissions, function (&$permission) {
array_walk($permissions, function (mixed &$permission) {
$permission = (string) $permission;
});
$this->collectedPermissions[$role->getName()] = array_values($permissions);
Expand Down Expand Up @@ -218,9 +222,13 @@ public function __serialize(): array

public function __unserialize(array $data): void
{
$this->collectedGuards = $data['guards'];
$this->collectedRoles = $data['roles'];
/** @psalm-suppress MixedAssignment*/
$this->collectedGuards = $data['guards'];
/** @psalm-suppress MixedAssignment*/
$this->collectedRoles = $data['roles'];
/** @psalm-suppress MixedAssignment*/
$this->collectedPermissions = $data['permissions'];
$this->collectedOptions = $data['options'];
/** @psalm-suppress MixedAssignment*/
$this->collectedOptions = $data['options'];
}
}
3 changes: 3 additions & 0 deletions test/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* and is licensed under the MIT license.
*/

use Composer\Autoload\ClassLoader;
use LmcTest\Rbac\Mvc\DevToolsTest\Util\ServiceManagerFactory;

ini_set('error_reporting', E_ALL);
Expand All @@ -29,6 +30,7 @@

foreach ($files as $file) {
if (file_exists($file)) {
/** @var ClassLoader $loader */
$loader = require $file;
break;
}
Expand All @@ -38,6 +40,7 @@
throw new RuntimeException('vendor/autoload.php could not be found. Did you install via composer?');
}

/** @var array $config */
$config = require __DIR__ . '/TestConfiguration.php';
ServiceManagerFactory::setApplicationConfig($config);
unset($files, $file, $loader, $config);
21 changes: 18 additions & 3 deletions test/Collector/RbacCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

namespace LmcTest\Rbac\Mvc\DevToolsTest\Collector;

use Exception;
use InvalidArgumentException;
use Laminas\Mvc\Application;
use Laminas\Mvc\ApplicationInterface;
use Laminas\Mvc\MvcEvent;
Expand All @@ -42,6 +44,7 @@
use LmcTest\Rbac\Mvc\DevToolsTest\Asset\MockRoleWithPermissionProperty;
use LmcTest\Rbac\Mvc\DevToolsTest\Asset\MockRoleWithPermissionTraversable;
use PHPUnit\Framework\TestCase;
use ReflectionException;

use function serialize;
use function unserialize;
Expand All @@ -58,11 +61,15 @@ public function testDefaultGetterReturnValues(): void
$this->assertSame('lmc_rbac', $collector->getName());
}

/**
* @throws Exception
*/
public function testSerialize(): void
{
$collector = new RbacCollector();
$serialized = $collector->serialize();
$this->assertIsString($serialized);
/** @var array $unserialized */
$unserialized = unserialize($serialized);
$this->assertSame([], $unserialized['guards']);
$this->assertSame([], $unserialized['roles']);
Expand All @@ -89,7 +96,7 @@ public function testUnserialize(): void
$serialized = serialize($unserialized);
$collector->unserialize($serialized);
$collection = $collector->getCollection();
$this->assertIsArray($collection);
// $this->assertIsArray($collection);
$this->assertSame(['foo' => 'bar'], $collection['guards']);
$this->assertSame(['foo' => 'bar'], $collection['roles']);
$this->assertSame(['foo' => 'bar'], $collection['options']);
Expand All @@ -98,13 +105,16 @@ public function testUnserialize(): void

public function testUnserializeThrowsInvalidArgumentException(): void
{
$this->expectException('InvalidArgumentException');
$this->expectException(InvalidArgumentException::class);
$collector = new RbacCollector();
$unserialized = 'not_an_array';
$serialized = serialize($unserialized);
$collector->unserialize($serialized);
}

/**
* @throws ReflectionException
*/
public function testCollectNothingIfNoApplicationIsSet(): void
{
$mvcEvent = new MvcEvent();
Expand All @@ -116,10 +126,15 @@ public function testCollectNothingIfNoApplicationIsSet(): void
'permissions' => [],
'options' => [],
];
$test = $collector->getCollection();
// $test = $collector->getCollection();
$this->assertEquals($expectedCollection, $collector->getCollection());
}

/**
* @throws ReflectionException
* @throws \PHPUnit\Framework\MockObject\Exception
* @throws Exception
*/
public function testCanCollect(): void
{
$dataToCollect = [
Expand Down
4 changes: 3 additions & 1 deletion test/ConfigProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
class ConfigProviderTest extends TestCase
{
public function testProvidesExpectedConfig()
public function testProvidesExpectedConfig(): void
{
$provider = new ConfigProvider();
$expectedDependencyConfig = [
Expand All @@ -36,12 +36,14 @@ public function testProvidesExpectedConfig()
],
],
];
/*
$expectedViewManagerConfig = [
'template_map' => [
'laminas-developer-tools/toolbar/lmc-rbac'
=> realpath(__DIR__ . '/../view/laminas-developer-tools/toolbar/lmc-rbac.phtml'),
],
];
*/
$this->assertEquals($expectedDependencyConfig, $provider->getDependencies());
$this->assertEquals($expectedLaminasDevtoolsConfig, $provider->getLaminasDeveloperToolsConfig());
// View Manager config
Expand Down

0 comments on commit b208009

Please sign in to comment.