Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial implementation #1

Draft
wants to merge 12 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/php-test-framework.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/service-tools.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
# Dhii - Service tools

A set of tools for working with service providers.
A set of tools for working with service providers that use [`ServiceInterface`](service-interface).

## Analysis

This package provides a set of analyzers that can detect various kinds of service-related issues, such as:

* Circular dependencies
* Dependencies that don't resolve to known services
* Extensions that don't extend a known service

**Usage**:

```
$analyzer = new CircularDependencyAnalyzer();
$issues = $analyzer->analyze($factories, $extensions);

foreach ($issues as $issue) {
$severity = $issue->getSeverity();
$message = $issue->getMessage();

switch ($severity) {
case Issue::WARNING: /* do something */
case Issue::ERROR: /* do something */
}
}
```

All the analyzers implement an [`AnalyzerInterface`](analyzer-interface). Container implementations can choose to
accept an analyzer instance during construction or setup using this interface, and then perform analysis on the
finished state of the container, after all [`ServiceProviderInterface`](service-provider) instances have been accepted.

Whether a container takes action on the reported issues or not is left up to the implementation. However, it is
strongly recommended that implementations take action according to the severity of the reported issues:

* `Issue::WARNING` - these issues should be reported to the developer; execution may resume.
* `Issue::ERROR` - these issues represent a fatal error waiting to happen; execution should probably stop.

[service-interface]: https://github.com/Dhii/services-interface
[analyzer-interface]: https://github.com/Dhii/service-tools/blob/initial/src/AnalyzerInterface.php
[service-provider]: https://github.com/container-interop/service-provider/blob/master/src/ServiceProviderInterface.php
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
}
],
"require": {
"php": "^7.2 | ^8.0"
"php": "^7.2 | ^8.0",
"dhii/services-interface": "0.1.x-dev",
"container-interop/service-provider": "dev-master"
},
"require-dev": {
"phpunit/phpunit": "^8.0 | ^9.0",
Expand All @@ -38,5 +40,9 @@
"branch-alias": {
"dev-initial": "0.1.x-dev"
}
},
"scripts": {
"phpcs": "vendor/bin/phpcs -s --runtime-set ignore_warnings_on_exit 1",
"psalm": "vendor/bin/psalm --show-info=true --threads=8 --diff"
}
}
192 changes: 142 additions & 50 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file removed src/.gitkeep
Empty file.
20 changes: 20 additions & 0 deletions src/AnalyzerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Dhii\Services\Tools;

use Dhii\Services\ServiceInterface;

interface AnalyzerInterface
{
/**
* Analyzes a set of factories and extensions.
*
* @param array<callable|ServiceInterface> $factories The list of factories to analyze.
* @param array<callable|ServiceInterface> $extensions The list of extensions to analyze.
Comment on lines +14 to +15
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these be iterable instead of array?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure. It's been a while since I wrote this. I'll get back to you on this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They can't be iterable because the desired type is, in actuality, array<string, callable|ServiceInterface>.

*
* @return iterable<Issue> A list of issues found during analysis.
*/
public function analyze(array $factories, array $extensions): iterable;
}
34 changes: 34 additions & 0 deletions src/Analyzers/AggregateAnalyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Dhii\Services\Tools\Analyzers;

use Dhii\Services\Tools\AnalyzerInterface;

/**
* An analyzer implementation that aggregates issues from multiple other analyzers.
*/
class AggregateAnalyzer implements AnalyzerInterface
{
/** @var AnalyzerInterface[] */
protected $analyzers;

/**
* Constructor.
*
* @param AnalyzerInterface[] $analyzers The analyzers to aggregate.
*/
public function __construct(array $analyzers)
{
$this->analyzers = $analyzers;
}

/** @inheritDoc */
public function analyze(array $factories, array $extensions): iterable
{
foreach ($this->analyzers as $analyzer) {
yield from $analyzer->analyze($factories, $extensions);
}
}
}
Loading