Skip to content

Commit

Permalink
fix: errors and warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed Oct 27, 2023
1 parent 193a8f4 commit 2819b9b
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .phplint.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
path: ./src
jobs: 10
cache: .cache/phplint.cache
cache: var/cache/phplint.cache
extensions:
- php
warning: false
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 9
tmpDir: .cache/phpstan
tmpDir: var/cache/phpstan
paths:
- src
9 changes: 0 additions & 9 deletions src/Exceptions/InvalidData.php

This file was deleted.

33 changes: 33 additions & 0 deletions src/Exceptions/InvalidResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Atoolo\Resource\Exceptions;

/**
* This exception is used when a resource is invalid. This can have the
* following reasons:
*
* - If the resource is syntactically incorrect.
* - If the resource does not contain necessary data.
*/
class InvalidResource extends \RuntimeException
{
public function __construct(

Check warning on line 16 in src/Exceptions/InvalidResource.php

View check run for this annotation

Codecov / codecov/patch

src/Exceptions/InvalidResource.php#L16

Added line #L16 was not covered by tests
private readonly string $location,
string $message = "",
int $code = 0,
?\Throwable $previous = null
) {
parent::__construct(
$location . ': ' . $message,
$code,
$previous
);

Check warning on line 26 in src/Exceptions/InvalidResource.php

View check run for this annotation

Codecov / codecov/patch

src/Exceptions/InvalidResource.php#L22-L26

Added lines #L22 - L26 were not covered by tests
}

public function getLocation(): string

Check warning on line 29 in src/Exceptions/InvalidResource.php

View check run for this annotation

Codecov / codecov/patch

src/Exceptions/InvalidResource.php#L29

Added line #L29 was not covered by tests
{
return $this->location;

Check warning on line 31 in src/Exceptions/InvalidResource.php

View check run for this annotation

Codecov / codecov/patch

src/Exceptions/InvalidResource.php#L31

Added line #L31 was not covered by tests
}
}
30 changes: 30 additions & 0 deletions src/Exceptions/ResourceNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Atoolo\Resource\Exceptions;

/**
* This exception is used if no resource could be found via the specified
* location.
*/
class ResourceNotFound extends \RuntimeException
{
public function __construct(

Check warning on line 13 in src/Exceptions/ResourceNotFound.php

View check run for this annotation

Codecov / codecov/patch

src/Exceptions/ResourceNotFound.php#L13

Added line #L13 was not covered by tests
private readonly string $location,
string $message = "",
int $code = 0,
?\Throwable $previous = null
) {
parent::__construct(
$location . ': ' . $message,
$code,
$previous
);

Check warning on line 23 in src/Exceptions/ResourceNotFound.php

View check run for this annotation

Codecov / codecov/patch

src/Exceptions/ResourceNotFound.php#L19-L23

Added lines #L19 - L23 were not covered by tests
}

public function getLocation(): string

Check warning on line 26 in src/Exceptions/ResourceNotFound.php

View check run for this annotation

Codecov / codecov/patch

src/Exceptions/ResourceNotFound.php#L26

Added line #L26 was not covered by tests
{
return $this->location;

Check warning on line 28 in src/Exceptions/ResourceNotFound.php

View check run for this annotation

Codecov / codecov/patch

src/Exceptions/ResourceNotFound.php#L28

Added line #L28 was not covered by tests
}
}
4 changes: 4 additions & 0 deletions src/Loader/SiteKit/ContextStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace Atoolo\Resource\Loader\SiteKit;

/**
* Provides the behavior of a context needed to load a SiteKit resource.
* @codeCoverageIgnore
*/
class ContextStub
{
public function redirectToTranslation(
Expand Down
10 changes: 10 additions & 0 deletions src/Loader/SiteKit/LifecylceStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@

namespace Atoolo\Resource\Loader\SiteKit;

/**
* Provides the behavior of a lifecylce needed to load a SiteKit resource.
* @codeCoverageIgnore
*/
class LifecylceStub
{
/**
* @param array<string, mixed> $data
*/
public function init(array $data): ResourceStub
{
$resource = new ResourceStub();
Expand All @@ -23,6 +30,9 @@ public function process(string $name, ResourceStub $resource): bool
return true;
}

/**
* @return array<string, mixed>
*/
public function service(ResourceStub $resource): array
{
return $resource->getData();
Expand Down
18 changes: 17 additions & 1 deletion src/Loader/SiteKit/ResourceStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,36 @@

namespace Atoolo\Resource\Loader\SiteKit;

/**
* Provides the behavior of a resource needed to load a SiteKit resource.
* @codeCoverageIgnore
*/
class ResourceStub
{
/**
* @var array<string, mixed>
*/
private array $data = [];

public function init($data)
/**
* @param array<string, mixed> $data
*/
public function init(array $data): void
{
$this->data['init'] = $data;
}

/**
* @param array<string, mixed> $data
*/
public function process(string $name, array $data): void
{
$this->data[$name] = $data;
}

/**
* @return array<string, mixed>
*/
public function getData(): array
{
return $this->data;
Expand Down
69 changes: 52 additions & 17 deletions src/Loader/SiteKitLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,85 @@

namespace Atoolo\Resource\Loader;

use Atoolo\Resource\Exceptions\InvalidData;
use Atoolo\Resource\Exceptions\InvalidResource;
use Atoolo\Resource\Exceptions\ResourceNotFound;
use Atoolo\Resource\Loader\SiteKit\ContextStub;
use Atoolo\Resource\Loader\SiteKit\LifecylceStub;
use Atoolo\Resource\Resource;
use Atoolo\Resource\ResourceLoader;

/**
* ResourceLoader that loads resources created with SiteKit aggregators.
*/
class SiteKitLoader implements ResourceLoader
{
public function __construct(private string $base)
{
public function __construct(
private readonly string $base
) {
}

public function load(string $location): Resource
{
$data = $this->loadRaw($this->base . '/' . $location);
$data = $this->loadRaw($location);

$this->validateData($location, $data);

$this->validateData($data);
$init = (array)$data['init'];

return new Resource(
$location,
(string)$data['init']['id'],
$data['init']['name'],
$data['init']['objectType'],
(string)$init['id'], // @phpstan-ignore-line
$init['name'], // @phpstan-ignore-line
$init['objectType'], // @phpstan-ignore-line
$data
);
}

private function loadRaw(string $file): array
/**
* @return array<string, mixed>
*/
private function loadRaw(string $location): array
{
$file = $this->base . '/' . $location;

$context = new ContextStub();
$lifecycle = new LifecylceStub();
return include $file;

$saveErrorReporting = error_reporting();

try {
error_reporting(E_ERROR | E_PARSE);
return require $file;
} catch (\ParseError $e) {
throw new InvalidResource($location, $e->getMessage(), 0, $e);
} catch (\Error $e) {
if (!file_exists($file)) {
throw new ResourceNotFound($location, $e->getMessage(), 0, $e);
}
throw new InvalidResource($location, $e->getMessage(), 0, $e);

Check warning on line 62 in src/Loader/SiteKitLoader.php

View check run for this annotation

Codecov / codecov/patch

src/Loader/SiteKitLoader.php#L62

Added line #L62 was not covered by tests
} finally {
error_reporting($saveErrorReporting);
}
}

private function validateData(array $data): void
/**
* @param array<string, mixed> $data
*/
private function validateData(string $location, array $data): void
{
if (!isset($data['init']['id'])) {
throw new InvalidData('id field missing');
$init = $data['init'];
if (!is_array($init)) {
throw new InvalidResource($location, 'missing init array');

Check warning on line 75 in src/Loader/SiteKitLoader.php

View check run for this annotation

Codecov / codecov/patch

src/Loader/SiteKitLoader.php#L75

Added line #L75 was not covered by tests
}

if (!isset($init['id'])) {
throw new InvalidResource($location, 'id field missing');
}
if (!isset($data['init']['name'])) {
throw new InvalidData('name field missing');
if (!isset($init['name'])) {
throw new InvalidResource($location, 'name field missing');
}
if (!isset($data['init']['objectType'])) {
throw new InvalidData('objectType field missing');
if (!isset($init['objectType'])) {
throw new InvalidResource($location, 'objectType field missing');
}
}
}
12 changes: 11 additions & 1 deletion src/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@

namespace Atoolo\Resource;

/**
* In the Atoolo context, resources are aggregated data from
* IES (Sitepark's content management system).
*/
class Resource
{
/**
* @param array<string, mixed> $data
*/
public function __construct(
private readonly string $location,
private readonly string $id,
Expand Down Expand Up @@ -40,11 +47,14 @@ public function getData(string $name): mixed
return $this->findData($this->data, $name);
}

/**
* @param array<string, mixed> $data
*/
private function findData(array $data, string $name): mixed
{
$names = explode('.', $name);
foreach ($names as $n) {
if (isset($data[$n])) {
if (is_array($data) && isset($data[$n])) {
$data = $data[$n];
} else {
return null;
Expand Down
4 changes: 4 additions & 0 deletions src/ResourceLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace Atoolo\Resource;

/**
* The ResourceLoader interface defines the method used to load resources from a
* given location.
*/
interface ResourceLoader
{
public function load(string $location): Resource;
Expand Down
23 changes: 18 additions & 5 deletions test/Loader/SiteKitLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Atoolo\Resource\Test\Loader;

use Atoolo\Resource\Exceptions\InvalidData;
use Atoolo\Resource\Exceptions\InvalidResource;
use Atoolo\Resource\Exceptions\ResourceNotFound;
use Atoolo\Resource\Loader\SiteKitLoader;
use Atoolo\Resource\Resource;
use PHPUnit\Framework\Attributes\CoversClass;
Expand All @@ -19,7 +20,7 @@ protected function setUp(): void
$this->loader = new SiteKitLoader($base);
}

public function testLoad(): void
public function testLoadValidResource(): void
{
$resource = $this->loader->load('validResource.php');
$this->assertEquals(
Expand All @@ -29,21 +30,33 @@ public function testLoad(): void
);
}

public function testLoadMissingLocation(): void
{
$this->expectException(ResourceNotFound::class);
$this->loader->load('notfound.php');
}

public function testLoadResourceWithCompileError(): void
{
$this->expectException(InvalidResource::class);
$this->loader->load('compileError.php');
}

public function testLoadWithMissingId(): void
{
$this->expectException(InvalidData::class);
$this->expectException(InvalidResource::class);
$this->loader->load('missingIdResource.php');
}

public function testLoadWithMissingName(): void
{
$this->expectException(InvalidData::class);
$this->expectException(InvalidResource::class);
$this->loader->load('missingNameResource.php');
}

public function testLoadWithMissingObjectType(): void
{
$this->expectException(InvalidData::class);
$this->expectException(InvalidResource::class);
$this->loader->load('missingObjectTypeResource.php');
}
}
Loading

0 comments on commit 2819b9b

Please sign in to comment.