Skip to content

Commit

Permalink
Fixed coverage and code issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Dropelikeit committed Jan 29, 2023
1 parent 74dde4d commit 6747907
Show file tree
Hide file tree
Showing 28 changed files with 48 additions and 54 deletions.
2 changes: 1 addition & 1 deletion psalm.xml
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
errorLevel="6"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
Expand Down
6 changes: 0 additions & 6 deletions src/ClassContainer.php
Expand Up @@ -37,9 +37,6 @@ public function compile(): void
$this->compiled = true;
}

/**
* @psalm-param class-string $id
*/
public function get(string $id): ClassItemInterface
{
$metadata = $this->classes->searchById($id);
Expand All @@ -50,9 +47,6 @@ public function get(string $id): ClassItemInterface
return $metadata;
}

/**
* @psalm-param class-string $id
*/
public function has(string $id): bool
{
return $this->classes->hasEntry($id);
Expand Down
6 changes: 0 additions & 6 deletions src/Dto/ObjectStoreInterface.php
Expand Up @@ -8,13 +8,7 @@ interface ObjectStoreInterface
{
public static function create(): self;

/**
* @param class-string $class
*/
public function append(string $class, object $object): void;

/**
* @param class-string $class
*/
public function searchById(string $class): null|object;
}
7 changes: 4 additions & 3 deletions src/Dto/ObjectStoreItem.php
Expand Up @@ -4,6 +4,8 @@

namespace MarcelStrahl\Container\Dto;

use function in_array;

final class ObjectStoreItem
{
/**
Expand All @@ -21,7 +23,6 @@ private function __construct(
}

/**
* @psalm-param string|class-string $id
* @psalm-param class-string $class
* @psalm-param list<class-string> $interfaces
* @psalm-param list<class-string> $abstracts
Expand Down Expand Up @@ -75,11 +76,11 @@ private function searchByClass(string $class): bool

private function searchByInterface(string $interface): bool
{
return \in_array($interface, $this->interfaces, true);
return in_array($interface, $this->interfaces, true);
}

private function searchByAbstract(string $abstract): bool
{
return \in_array($abstract, $this->abstracts, true);
return in_array($abstract, $this->abstracts, true);
}
}
3 changes: 2 additions & 1 deletion src/Exception/CannotRetrieveException.php
Expand Up @@ -5,9 +5,10 @@
namespace MarcelStrahl\Container\Exception;

use Psr\Container\ContainerExceptionInterface;
use RuntimeException;
use Webmozart\Assert\Assert;

final class CannotRetrieveException extends \RuntimeException implements ContainerExceptionInterface
final class CannotRetrieveException extends RuntimeException implements ContainerExceptionInterface
{
/**
* @psalm-param class-string $class
Expand Down
3 changes: 2 additions & 1 deletion src/Exception/FileLoader/AdapterBuilderException.php
Expand Up @@ -4,12 +4,13 @@

namespace MarcelStrahl\Container\Exception\FileLoader;

use RuntimeException;
use Webmozart\Assert\Assert;

/**
* @author Marcel Strahl <info@marcel-strahl.de>
*/
final class AdapterBuilderException extends \RuntimeException
final class AdapterBuilderException extends RuntimeException
{
public static function createByUnknownAdapterType(string $adapter): self
{
Expand Down
3 changes: 2 additions & 1 deletion src/Exception/NonExistingFileException.php
Expand Up @@ -4,12 +4,13 @@

namespace MarcelStrahl\Container\Exception;

use RuntimeException;
use Webmozart\Assert\Assert;

/**
* @author Marcel Strahl <info@marcel-strahl.de>
*/
final class NonExistingFileException extends \RuntimeException
final class NonExistingFileException extends RuntimeException
{
/**
* @psalm-param non-empty-string $file
Expand Down
3 changes: 2 additions & 1 deletion src/Exception/NotFoundInContainerException.php
Expand Up @@ -6,11 +6,12 @@

use Psr\Container\NotFoundExceptionInterface;
use RuntimeException;
use Throwable;
use Webmozart\Assert\Assert;

final class NotFoundInContainerException extends RuntimeException implements NotFoundExceptionInterface
{
public static function create(string $id, ?\LogicException $previousException = null): self
public static function create(string $id, ?Throwable $previousException = null): self
{
$message = sprintf('Can not found a entry in container with given id "%s"', $id);
Assert::stringNotEmpty($message);
Expand Down
4 changes: 3 additions & 1 deletion src/Exception/NotInitializedException.php
Expand Up @@ -4,7 +4,9 @@

namespace MarcelStrahl\Container\Exception;

final class NotInitializedException extends \LogicException
use LogicException;

final class NotInitializedException extends LogicException
{
/**
* @param class-string $class
Expand Down
Expand Up @@ -4,11 +4,10 @@

namespace MarcelStrahl\Container\Exception\ObjectBuilder;

final class CanNotCreateClassWithNoneClassDependencies extends \LogicException
use LogicException;

final class CanNotCreateClassWithNoneClassDependencies extends LogicException
{
/**
* @psalm-param non-empty-string $type
*/
public static function create(string $type): self
{
return new self(sprintf(
Expand Down
3 changes: 2 additions & 1 deletion src/Exception/UnknownFileExtensionException.php
Expand Up @@ -4,12 +4,13 @@

namespace MarcelStrahl\Container\Exception;

use RuntimeException;
use Webmozart\Assert\Assert;

/**
* @author Marcel Strahl <info@marcel-strahl.de>
*/
final class UnknownFileExtensionException extends \RuntimeException
final class UnknownFileExtensionException extends RuntimeException
{
/**
* @psalm-param non-empty-string $path
Expand Down
7 changes: 6 additions & 1 deletion src/FileLoader/PHPArrayAdapter.php
Expand Up @@ -4,10 +4,15 @@

namespace MarcelStrahl\Container\FileLoader;

use function array_walk;
use function call_user_func_array;
use function file_exists;
use MarcelStrahl\Container\Dto\ClassStore;

use MarcelStrahl\Container\Dto\ClassStoreInterface;
use MarcelStrahl\Container\Exception\NonExistingFileException;
use MarcelStrahl\Container\Exception\UnknownFileExtensionException;
use function str_contains;

final class PHPArrayAdapter implements FileLoader
{
Expand Down Expand Up @@ -39,7 +44,7 @@ public function loadFileFromPaths(array $paths, ClassStoreInterface $classStore)
array_walk(
$paths,
static function (string $path) use ($instance, $classStore): void {
\call_user_func_array([$instance, 'loadFileFromPath'], [$path, $classStore]);
call_user_func_array([$instance, 'loadFileFromPath'], [$path, $classStore]);
}
);

Expand Down
14 changes: 10 additions & 4 deletions src/ObjectBuilder/ReflectionBuilder.php
Expand Up @@ -4,19 +4,25 @@

namespace MarcelStrahl\Container\ObjectBuilder;

use LogicException;
use MarcelStrahl\Container\Exception\ObjectBuilder\CanNotCreateClassWithNoneClassDependencies;
use ReflectionClass;
use ReflectionException;
use ReflectionParameter;
use Webmozart\Assert\Assert;

final class ReflectionBuilder implements ObjectBuilder
{
public function initialize(string $class): object
{
try {
$reflectionClass = new \ReflectionClass($class);
} catch (\ReflectionException $exception) {
throw new \LogicException('Cannot find your class, try `composer dumpautoload` command.');
$reflectionClass = new ReflectionClass($class);
} catch (ReflectionException $exception) {
throw new LogicException('Cannot find your class, try `composer dumpautoload` command.');
}

Assert::classExists($class);

$parameters = $reflectionClass->getConstructor()?->getParameters();

// Current class has no constructor and can initialize directly, or it has a constructor but no dependencies.
Expand All @@ -35,7 +41,7 @@ public function initialize(string $class): object
}

/**
* @param array<int, \ReflectionParameter> $parameters
* @param array<int, ReflectionParameter> $parameters
*
* @psalm-param list<ReflectionParameter> $parameters
*
Expand Down
9 changes: 2 additions & 7 deletions src/ObjectContainer.php
Expand Up @@ -4,6 +4,7 @@

namespace MarcelStrahl\Container;

use LogicException;
use MarcelStrahl\Container\Dto\ObjectStoreInterface;
use MarcelStrahl\Container\Exception\NotFoundInContainerException;
use MarcelStrahl\Container\ObjectBuilder\ObjectBuilder;
Expand All @@ -17,9 +18,6 @@ public function __construct(
) {
}

/**
* @param class-string $id
*/
public function get(string $id): object
{
$object = $this->objectStore->searchById($id);
Expand All @@ -29,7 +27,7 @@ public function get(string $id): object

try {
$object = $this->builder->initialize($id);
} catch (\LogicException $exception) {
} catch (LogicException $exception) {
throw NotFoundInContainerException::create($id, $exception);
}

Expand All @@ -38,9 +36,6 @@ public function get(string $id): object
return $object;
}

/**
* @param class-string $id
*/
public function has(string $id): bool
{
return null !== $this->objectStore->searchById($id);
Expand Down
2 changes: 1 addition & 1 deletion tests/Acceptance/WorkflowTest.php
Expand Up @@ -22,7 +22,7 @@
*
* @internal
*
* @coversNothing
*/
final class WorkflowTest extends TestCase
{
Expand Down
1 change: 0 additions & 1 deletion tests/AppContainerTest.php
Expand Up @@ -18,7 +18,6 @@
*
* @internal
*
* @coversNothing
*/
final class AppContainerTest extends TestCase
{
Expand Down
1 change: 0 additions & 1 deletion tests/ClassContainerTest.php
Expand Up @@ -15,7 +15,6 @@
/**
* @internal
*
* @coversNothing
*/
final class ClassContainerTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Dto/ClassItemTest.php
Expand Up @@ -13,7 +13,7 @@
/**
* @internal
*
* @coversNothing
*/
final class ClassItemTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Dto/ClassStoreTest.php
Expand Up @@ -13,7 +13,7 @@
/**
* @internal
*
* @coversNothing
*/
final class ClassStoreTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Dto/ObjectStoreItemTest.php
Expand Up @@ -14,7 +14,7 @@
/**
* @internal
*
* @coversNothing
*/
final class ObjectStoreItemTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Dto/ObjectStoreTest.php
Expand Up @@ -11,7 +11,7 @@
/**
* @internal
*
* @coversNothing
*/
final class ObjectStoreTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Exception/NotFoundInContainerExceptionTest.php
Expand Up @@ -11,7 +11,7 @@
/**
* @internal
*
* @coversNothing
*/
final class NotFoundInContainerExceptionTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Exception/NotInitializedExceptionTest.php
Expand Up @@ -10,7 +10,7 @@
/**
* @internal
*
* @coversNothing
*/
final class NotInitializedExceptionTest extends TestCase
{
Expand Down
Expand Up @@ -10,7 +10,7 @@
/**
* @internal
*
* @coversNothing
*/
final class CanNotCreateClassWithNoneClassDependenciesTest extends TestCase
{
Expand Down
1 change: 0 additions & 1 deletion tests/FileLoader/AdapterBuilderTest.php
Expand Up @@ -15,7 +15,6 @@
*
* @internal
*
* @coversNothing
*/
final class AdapterBuilderTest extends TestCase
{
Expand Down
2 changes: 0 additions & 2 deletions tests/FileLoader/PhpArrayAdapterTest.php
Expand Up @@ -17,8 +17,6 @@
* @author Marcel Strahl <info@marcel-strahl.de>
*
* @internal
*
* @coversNothing
*/
final class PhpArrayAdapterTest extends TestCase
{
Expand Down
3 changes: 1 addition & 2 deletions tests/ObjectBuilder/ReflectionBuilderTest.php
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace MarcelStrahl\Tests\Container\ObjectBuilder;
namespace MarcelStrahl\Tests\ObjectBuilder;

use MarcelStrahl\Container\Exception\ObjectBuilder\CanNotCreateClassWithNoneClassDependencies;
use MarcelStrahl\Container\ObjectBuilder\ObjectBuilder;
Expand All @@ -16,7 +16,6 @@
/**
* @internal
*
* @coversNothing
*/
final class ReflectionBuilderTest extends TestCase
{
Expand Down
1 change: 0 additions & 1 deletion tests/ObjectContainerTest.php
Expand Up @@ -15,7 +15,6 @@
/**
* @internal
*
* @coversNothing
*/
final class ObjectContainerTest extends TestCase
{
Expand Down

0 comments on commit 6747907

Please sign in to comment.