Skip to content

Commit

Permalink
Merge branch 'release/v0.2.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
betterthanclay committed Sep 30, 2020
2 parents ba3b563 + 4d0e698 commit 063ae69
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v0.2.1 (2020-09-30)
* Fixed type inference for existing interfaces

## v0.2.0 (2020-09-29)
* Added initial Exception Factory structure
* Improved interface list handling
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"extra": {
"branch-alias": {
"dev-develop": "0.1.x-dev"
"dev-develop": "0.2.x-dev"
}
}
}
60 changes: 33 additions & 27 deletions src/Exceptional/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,10 @@ protected function importTypes(array $types): void
}

// Named class
if (class_exists($type)) {
if (
class_exists($type) &&
is_a($type, \Exception::class, true)
) {
if ($this->baseClass !== null) {
throw new InvalidArgumentException(
'Exception has already defined base type: '.$this->baseClass
Expand Down Expand Up @@ -450,13 +453,18 @@ protected function indexInterface(string $interface): void
}

// Namespace
$namespaceParent = null;

if (!empty($parts)) {
$this->indexNamespaceInterfaces($parts);
$namespaceParent = $this->indexNamespaceInterfaces($parts);
}


// Interface
if ($classExists = class_exists($interface)) {
if (
($classExists = class_exists($interface)) &&
is_a($interface, \Exception::class, true)
) {
$baseClass = trim($interface, '\\');

if (
Expand Down Expand Up @@ -485,8 +493,8 @@ protected function indexInterface(string $interface): void
'\\DecodeLabs\\Exceptional\\'.$name.'Exception'
];

if (!empty($parts)) {
$this->interfaceIndex[$interface][] = '\\'.implode('\\', $parts).'\\Exception';
if ($namespaceParent !== null) {
$this->interfaceIndex[$interface][] = $namespaceParent;
}
}
} else {
Expand All @@ -495,42 +503,46 @@ protected function indexInterface(string $interface): void
!$classExists &&
!isset($this->interfaceIndex[$interface])
) {
$this->interfaceIndex[$interface] = [
'\\DecodeLabs\\Exceptional\\Exception'
];
$this->interfaceIndex[$interface] = ['\\'.Exception::class];
}
}
}

/**
* Index namespace interface
*/
protected function indexNamespaceInterfaces(array $parts): void
protected function indexNamespaceInterfaces(array $parts): ?string
{
$set = [];
$last = '\\DecodeLabs\\Exceptional\\Exception';
$first = $last = '\\DecodeLabs\\Exceptional\\Exception';

while (!empty($parts)) {
$set[] = array_shift($parts);
$interface = '\\'.implode('\\', $set).'\\Exception';

// Check
if (class_exists($interface)) {
throw new LogicException(
'Requested interface exists as a class: '.$interface
);
// We have to skip
continue;
}

if (
isset($this->interfaceIndex[$interface]) ||
interface_exists($interface)
) {
$last = $interface;
continue;
}

$this->interfaceIndex[$interface] = [$last];
$last = $interface;
}

if ($last !== $first) {
return $last;
} else {
return null;
}
}


Expand All @@ -544,18 +556,7 @@ protected function indexPackageInterface(string $name): void
$interface = $prefix.$name.'Exception';

// Check
if (class_exists($interface)) {
throw new LogicException(
'Requested interface exists as a class: '.$interface
);
}

if (
isset($this->interfaceIndex[$interface]) ||
interface_exists($interface)
) {
return;
}
$classExists = class_exists($interface);

// Base class
if (
Expand All @@ -577,9 +578,14 @@ interface_exists($interface)
// Interface
if (isset($standard['extend'])) {
$this->indexPackageInterface($standard['extend']);
$this->interfaceIndex[$interface] = [$prefix.$standard['extend'].'Exception'];

if (!$classExists) {
$this->interfaceIndex[$interface] = [$prefix.$standard['extend'].'Exception'];
}
} else {
$this->interfaceIndex[$interface] = [$prefix.'Exception'];
if (!$classExists) {
$this->interfaceIndex[$interface] = [$prefix.'Exception'];
}
}
}

Expand Down

0 comments on commit 063ae69

Please sign in to comment.