Skip to content

Commit

Permalink
Code fixes for PHP compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
wpscholar committed Apr 29, 2023
1 parent cd01885 commit 9c35f26
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 22 deletions.
53 changes: 31 additions & 22 deletions includes/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Closure;
use Countable;
use Iterator;
use Psr\Container\ContainerInterface;
use SplObjectStorage;

/**
Expand Down Expand Up @@ -94,7 +93,7 @@ public function reset() {
*
* @return bool
*/
public function has( string $id ) {
public function has( $id ) {
return array_key_exists( $id, $this->entries );
}

Expand All @@ -107,7 +106,7 @@ public function has( string $id ) {
*
* @throws NotFoundException
*/
public function raw( string $id ) {
public function raw( $id ) {
if ( ! $this->has( $id ) ) {
throw new NotFoundException( sprintf( 'No entry was found for "%s" identifier.', $id ) );
}
Expand All @@ -124,7 +123,7 @@ public function raw( string $id ) {
*
* @throws NotFoundException No entry was found for **this** identifier.
*/
public function get( string $id ) {
public function get( $id ) {
// Return class instance, if available.
if ( isset( $this->instances[ $id ] ) ) {
return $this->instances[ $id ];
Expand Down Expand Up @@ -156,12 +155,12 @@ public function get( string $id ) {
/**
* Set an array value by ID.
*
* @param string $id The entry identifier.
* @param mixed $value The entry value.
* @param string $id The entry identifier.
* @param mixed $value The entry value.
*
* @return $this
*/
public function set( string $id, $value ) {
public function set( $id, $value ) {
$this->entries[ $id ] = $value;

return $this;
Expand All @@ -176,7 +175,7 @@ public function set( string $id, $value ) {
*
* @throws NotFoundException
*/
public function delete( string $id ) {
public function delete( $id ) {
if ( $this->has( $id ) ) {
$value = $this->get( $id );
if ( $this->isFactory( $value ) ) {
Expand All @@ -199,7 +198,7 @@ public function delete( string $id ) {
*
* @return $this
*/
public function deleteInstance( string $id ) {
public function deleteInstance( $id ) {
unset( $this->instances[ $id ] );

return $this;
Expand All @@ -219,15 +218,15 @@ public function deleteAllInstances() {
/**
* Extend a factory or service by creating a closure that will manipulate the instantiated instance.
*
* @param string $id
* @param string $id
* @param Closure $closure
*
* @return Closure
*
* @throws ContainerException
* @throws NotFoundException
*/
public function extend( string $id, Closure $closure ) {
public function extend( $id, Closure $closure ) {

// Get the existing raw value
$value = $this->raw( $id );
Expand Down Expand Up @@ -358,7 +357,8 @@ public function keys() {
*
* @throws NotFoundException No entry was found for **this** identifier.
*/
public function current(): mixed {
#[\ReturnTypeWillChange]
public function current() {
return $this->offsetGet( $this->key() );
}

Expand All @@ -369,7 +369,8 @@ public function current(): mixed {
*
* @return void Any returned value is ignored.
*/
public function next(): void {
#[\ReturnTypeWillChange]
public function next() {
++ $this->pointer;
}

Expand All @@ -380,7 +381,8 @@ public function next(): void {
*
* @return string The entry identifier for the current entry.
*/
public function key(): mixed {
#[\ReturnTypeWillChange]
public function key() {
return $this->keys()[ $this->pointer ];
}

Expand All @@ -391,7 +393,8 @@ public function key(): mixed {
*
* @return bool The return value will be cast to boolean and then evaluated.
*/
public function valid(): bool {
#[\ReturnTypeWillChange]
public function valid() {
return isset( $this->keys()[ $this->pointer ] );
}

Expand All @@ -402,7 +405,8 @@ public function valid(): bool {
*
* @return void Any returned value is ignored.
*/
public function rewind(): void {
#[\ReturnTypeWillChange]
public function rewind() {
$this->pointer = 0;
}

Expand All @@ -415,7 +419,8 @@ public function rewind(): void {
*
* @return bool True on success or false on failure.
*/
public function offsetExists( mixed $offset ): bool {
#[\ReturnTypeWillChange]
public function offsetExists( $offset ) {
return $this->has( $offset );
}

Expand All @@ -430,7 +435,8 @@ public function offsetExists( mixed $offset ): bool {
*
* @throws NotFoundException No entry was found for **this** identifier.
*/
public function offsetGet( mixed $offset ): mixed {
#[\ReturnTypeWillChange]
public function offsetGet( $offset ) {
return $this->get( $offset );
}

Expand All @@ -440,11 +446,12 @@ public function offsetGet( mixed $offset ): mixed {
* Method implements ArrayAccess.
*
* @param mixed $offset The offset to assign the value to.
* @param mixed $value The value to set.
* @param mixed $value The value to set.
*
* @return void
*/
public function offsetSet( mixed $offset, mixed $value ): void {
#[\ReturnTypeWillChange]
public function offsetSet( $offset, $value ) {
$this->set( $offset, $value );
}

Expand All @@ -459,7 +466,8 @@ public function offsetSet( mixed $offset, mixed $value ): void {
*
* @throws NotFoundException
*/
public function offsetUnset( mixed $offset ): void {
#[\ReturnTypeWillChange]
public function offsetUnset( $offset ) {
$this->delete( $offset );
}

Expand All @@ -470,7 +478,8 @@ public function offsetUnset( mixed $offset ): void {
*
* @return int
*/
public function count(): int {
#[\ReturnTypeWillChange]
public function count() {
return count( $this->entries );
}
}
11 changes: 11 additions & 0 deletions includes/ContainerExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace WP_Forge\Container;

use Throwable;

/**
* Base interface representing a generic exception in a container.
*/
interface ContainerExceptionInterface extends Throwable {
}
34 changes: 34 additions & 0 deletions includes/ContainerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace WP_Forge\Container;

/**
* Describes the interface of a container that exposes methods to read its entries.
*/
interface ContainerInterface
{
/**
* Finds an entry of the container by its identifier and returns it.
*
* @param string $id Identifier of the entry to look for.
*
* @throws NotFoundExceptionInterface No entry was found for **this** identifier.
* @throws ContainerExceptionInterface Error while retrieving the entry.
*
* @return mixed Entry.
*/
public function get($id);

/**
* Returns true if the container can return an entry for the given identifier.
* Returns false otherwise.
*
* `has($id)` returning true does not mean that `get($id)` will not throw an exception.
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
*
* @param string $id Identifier of the entry to look for.
*
* @return bool
*/
public function has($id);
}
9 changes: 9 additions & 0 deletions includes/NotFoundExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace WP_Forge\Container;

/**
* No entry was found in the container.
*/
interface NotFoundExceptionInterface extends ContainerExceptionInterface {
}

0 comments on commit 9c35f26

Please sign in to comment.