From ac0ab9041ba8ca58858332422bf89a92acaf281f Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 6 Oct 2015 12:43:45 +0200 Subject: [PATCH] DibiObject: fixed compatibility with PHP 7 --- dibi/libs/DibiObject.php | 16 ++++----- tests/dibi/DibiObject.phpt | 70 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 tests/dibi/DibiObject.phpt diff --git a/dibi/libs/DibiObject.php b/dibi/libs/DibiObject.php index b4f75e6b..26764bdc 100644 --- a/dibi/libs/DibiObject.php +++ b/dibi/libs/DibiObject.php @@ -209,8 +209,8 @@ abstract class DibiObject } // property getter support - $name[0] = $name[0] & "\xDF"; // case-sensitive checking, capitalize first character - $m = 'get' . $name; + $uname = ucfirst($name); + $m = 'get' . $uname; if (self::hasAccessor($class, $m)) { // ampersands: // - uses & __get() because declaration should be forward compatible (e.g. with Nette\Web\Html) @@ -219,13 +219,12 @@ abstract class DibiObject return $val; } - $m = 'is' . $name; + $m = 'is' . $uname; if (self::hasAccessor($class, $m)) { $val = $this->$m(); return $val; } - $name = func_get_arg(0); throw new LogicException("Cannot read an undeclared property $class::\$$name."); } @@ -246,20 +245,18 @@ abstract class DibiObject } // property setter support - $name[0] = $name[0] & "\xDF"; // case-sensitive checking, capitalize first character - if (self::hasAccessor($class, 'get' . $name) || self::hasAccessor($class, 'is' . $name)) { + $uname = ucfirst($name); + if (self::hasAccessor($class, 'get' . $uname) || self::hasAccessor($class, 'is' . $uname)) { $m = 'set' . $name; if (self::hasAccessor($class, $m)) { $this->$m($value); return; } else { - $name = func_get_arg(0); throw new LogicException("Cannot assign to a read-only property $class::\$$name."); } } - $name = func_get_arg(0); throw new LogicException("Cannot assign to an undeclared property $class::\$$name."); } @@ -271,8 +268,7 @@ abstract class DibiObject */ public function __isset($name) { - $name[0] = $name[0] & "\xDF"; - return $name !== '' && self::hasAccessor(get_class($this), 'get' . $name); + return $name !== '' && self::hasAccessor(get_class($this), 'get' . ucfirst($name)); } diff --git a/tests/dibi/DibiObject.phpt b/tests/dibi/DibiObject.phpt new file mode 100644 index 00000000..58da69a1 --- /dev/null +++ b/tests/dibi/DibiObject.phpt @@ -0,0 +1,70 @@ +undeclared(); +}, 'LogicException', 'Call to undefined method TestClass::undeclared().'); + +Assert::exception(function () { + TestClass::undeclared(); +}, 'LogicException', 'Call to undefined static method TestClass::undeclared().'); + + +// writing +Assert::exception(function () { + $obj = new TestClass; + $obj->undeclared = 'value'; +}, 'LogicException', 'Cannot assign to an undeclared property TestClass::$undeclared.'); + + +// property getter +$obj = new TestClass; +Assert::true(isset($obj->bar)); +Assert::same(123, $obj->bar); +Assert::false(isset($obj->foo)); +Assert::same(456, $obj->foo); + + +// reading +Assert::exception(function () { + $obj = new TestClass; + $val = $obj->undeclared; +}, 'LogicException', 'Cannot read an undeclared property TestClass::$undeclared.'); + + +// unset/isset +Assert::exception(function () { + $obj = new TestClass; + unset($obj->undeclared); +}, 'LogicException', 'Cannot unset the property TestClass::$undeclared.'); + +Assert::false(isset($obj->undeclared)); + + +// extension method +TestClass::extensionMethod('join', $func = function (TestClass $that, $separator) { + return $that->foo . $separator . $that->bar; +}); + +$obj = new TestClass; +Assert::same('456*123', $obj->join('*'));