Skip to content

Commit

Permalink
Warn if property does not exist and add it to DiContainerTrait (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Oct 4, 2021
1 parent cfd9cf7 commit 6cd61da
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/DiContainerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
*/
trait DiContainerTrait
{
use WarnDynamicPropertyTrait;

/**
* Call from __construct() to initialize the properties allowing
* developer to pass Dependency Injector Container.
Expand Down
2 changes: 2 additions & 0 deletions src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/
class Exception extends \Exception
{
use WarnDynamicPropertyTrait;

/** @var array */
public $params = [];

Expand Down
51 changes: 51 additions & 0 deletions src/WarnDynamicPropertyTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Atk4\Core;

/**
* This trait implements https://github.com/php/php-src/pull/7390 for lower PHP versions
* and also emit a warning when isset() is called on undefined variable.
*/
trait WarnDynamicPropertyTrait
{
protected function warnPropertyDoesNotExist(string $name): void
{
'trigger_error'('Property ' . static::class . '::$' . $name . ' does not exist', \E_USER_DEPRECATED);
}

public function __isset(string $name): bool
{
$this->warnPropertyDoesNotExist($name);

return isset($this->{$name});
}

/**
* @return mixed
*/
public function &__get(string $name)
{
$this->warnPropertyDoesNotExist($name);

return $this->{$name};
}

/**
* @param mixed $value
*/
public function __set(string $name, $value): void
{
$this->warnPropertyDoesNotExist($name);

$this->{$name} = $value;
}

public function __unset(string $name): void
{
$this->warnPropertyDoesNotExist($name);

unset($this->{$name});
}
}

0 comments on commit 6cd61da

Please sign in to comment.