Skip to content

Commit

Permalink
feat: PHP 8.1 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Fanger committed Aug 7, 2022
1 parent 32298ca commit c326c91
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 193 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
"files": [
"src/functions.php"
]
},
"scripts": {
"test": "phpunit"
}
}
66 changes: 66 additions & 0 deletions composer.lock

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

2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<phpunit bootstrap="vendor/autoload.php"
<phpunit bootstrap="vendor/sledgehammer/core/tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
Expand Down
19 changes: 9 additions & 10 deletions src/HasManyPlaceholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ArrayAccess;
use Countable;
use Exception;
use Iterator;
use IteratorAggregate;
use Sledgehammer\Core\Collection;
use Sledgehammer\Core\Base;
Expand Down Expand Up @@ -47,44 +48,42 @@ public function __clone()

// @todo: mimic array errors and behavior on propery access and method invocation
// Array access
public function offsetExists($offset)
public function offsetExists($offset): bool
{
$this->replacePlaceholder();

return $this->__placeholder->offsetExists($offset);
}

public function offsetGet($offset)
public function offsetGet($offset): mixed
{
$this->replacePlaceholder();

return $this->__placeholder->offsetGet($offset);
}

public function offsetSet($offset, $value)
public function offsetSet($offset, $value): void
{
$this->replacePlaceholder();

return $this->__placeholder->offsetSet($offset, $value);
$this->__placeholder->offsetSet($offset, $value);
}

public function offsetUnset($offset)
public function offsetUnset($offset): void
{
$this->replacePlaceholder();

return $this->__placeholder->offsetUnset($offset);
$this->__placeholder->offsetUnset($offset);
}

// IteratorAggregate
public function getIterator()
public function getIterator(): Iterator
{
$this->replacePlaceholder();

return $this->__placeholder->getIterator();
}

// Countable
public function count()
public function count(): int
{
$this->replacePlaceholder();

Expand Down
28 changes: 15 additions & 13 deletions src/Junction.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __get($property)
{
if (property_exists($this->instance, $property)) {
if (array_key_exists($property, $this->fields)) {
notice('Property "'.$property.'" is ambiguous. It\'s available in both the instance as the junction fields.', "To modify the mapping of the junction field change the value of \$ModelConfig->hasMany[\$relation]['fields']['".$property."']");
notice('Property "' . $property . '" is ambiguous. It\'s available in both the instance as the junction fields.', "To modify the mapping of the junction field change the value of \$ModelConfig->hasMany[\$relation]['fields']['" . $property . "']");
}

return $this->instance->$property;
Expand All @@ -72,7 +72,7 @@ public function __get($property)
}
$properties = \Sledgehammer\reflect_properties($this->instance);
$properties['public'] = array_merge($properties['public'], $this->fields);
warning('Property "'.$property.'" doesn\'t exist in a '.get_class($this).' ('.get_class($this->instance).') object', \Sledgehammer\build_properties_hint($properties));
warning('Property "' . $property . '" doesn\'t exist in a ' . get_class($this) . ' (' . get_class($this->instance) . ') object', \Sledgehammer\build_properties_hint($properties));
}

/**
Expand All @@ -85,7 +85,7 @@ public function __set($property, $value)
{
if (property_exists($this->instance, $property)) {
if (array_key_exists($property, $this->fields)) {
notice('Property "'.$property.'" is ambiguous. It\'s available in both the instance as the junction fields.', "To modify the mapping of the junction field change the value of \$ModelConfig->hasMany[\$relation]['fields']['".$property."']");
notice('Property "' . $property . '" is ambiguous. It\'s available in both the instance as the junction fields.', "To modify the mapping of the junction field change the value of \$ModelConfig->hasMany[\$relation]['fields']['" . $property . "']");
}
$this->instance->$property = $value;

Expand All @@ -111,34 +111,36 @@ public function __call($method, $arguments)
{
return call_user_func_array([$this->instance, $method], $arguments);
}
public function offsetExists($index)
public function offsetExists($index): bool
{
if ($this->instance instanceof ArrayAccess) {
return $this->instance->offsetExists($index);
}
trigger_error('Cannot use object of type '.get_class($this->instance).' as array', E_USER_ERROR);
trigger_error('Cannot use object of type ' . get_class($this->instance) . ' as array', E_USER_ERROR);
}

public function offsetGet($index)
public function offsetGet($index): mixed
{
if ($this->instance instanceof ArrayAccess) {
return $this->instance->offsetGet($index);
}
trigger_error('Cannot use object of type '.get_class($this->instance).' as array', E_USER_ERROR);
trigger_error('Cannot use object of type ' . get_class($this->instance) . ' as array', E_USER_ERROR);
}

public function offsetSet($index, $value)
public function offsetSet($index, $value): void
{
if ($this->instance instanceof ArrayAccess) {
return $this->instance->offsetSet($index, $value);
$this->instance->offsetSet($index, $value);
return;
}
trigger_error('Cannot use object of type '.get_class($this->instance).' as array', E_USER_ERROR);
trigger_error('Cannot use object of type ' . get_class($this->instance) . ' as array', E_USER_ERROR);
}
public function offsetUnset($index)
public function offsetUnset($index): void
{
if ($this->instance instanceof ArrayAccess) {
return $this->instance->offsetUnset($index);
$this->instance->offsetUnset($index);
return;
}
trigger_error('Cannot use object of type '.get_class($this->instance).' as array', E_USER_ERROR);
trigger_error('Cannot use object of type ' . get_class($this->instance) . ' as array', E_USER_ERROR);
}
}
Loading

0 comments on commit c326c91

Please sign in to comment.