Bug #79440 [NEW]: Object typehint sometimes seems to be misunderstood
| From: | lmprogg at gmail dot com | Date: | Tue, 31 Mar 2020 22:18:50 +0000 |
| Subject: | Bug #79440 [NEW]: Object typehint sometimes seems to be misunderstood | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-226374@lists.php.net to get a copy of this message | ||
From: lmprogg at gmail dot com
Operating system: Ubuntu
PHP version: 7.3.16
Package: Class/Object related
Bug Type: Bug
Bug description:Object typehint sometimes seems to be misunderstood
Description:
------------
It is difficult for me to make this reproducible.
Short notice on the setup:
A local vm with vagrant and php 7.3.16:
PHP 7.3.16-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Mar 20 2020
13:51:21) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.16, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.16-1+ubuntu16.04.1+deb.sury.org+1, Copyright
(c) 1999-2018, by Zend Technologies
That one does *not* have the problem.
Then there is an aws instance that is running the following version:
PHP 7.3.16-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Mar 20 2020
13:51:46) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.16, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.16-1+ubuntu18.04.1+deb.sury.org+1, Copyright
(c) 1999-2018, by Zend Technologies
This one *has* the problem.
The following happens (I also included a little code snippet, although
it may be hard to reproduce).
Whenever a typehint "object" is encountered (in my failing case, it is
PHPUnit, but it happens on any typehint "object", even in other
functions):
[TypeError] Argument 5 passed to
PHPUnit\Framework\MockObject\Invocation::__construct() must be an
instance of PHPUnit\Framework\MockObject\object, instance of
Mock_MyClient_070b46a8 given, called in
/project/vendor/phpunit/phpunit/src/Framework/MockObject/MockClass.php(42)
: eval()'d code on line 47
This is the line that does not pass the typecheck: (PHPUnit 9.0.2)
public function __construct(string $className, string $methodName, array
$parameters, string $returnType, object $object, bool $cloneObjects =
false, bool $proxiedCall = false)
The 5th parameter seems to be misunderstood to be an "object" of the
same namespace, instead of the reserved "object" keyword. (which should
be passed by the mock passed in)
I first had this in 7.3.9, and then upgraded to the latest 7.3.16, but
the problem still persisted on the AWS instance.
I am not sure if this is a bug or just a configuration issue, but it
seems that some versions (I am not sure how they differ) are unable to
understand this relatively new typehint which was introduced in php 7.2.
I included a little snippet that *might* reproduce it. If you need more
info, please tell me, it is just a little hard to get it from the aws
instance (which is why I did not go haywire and collected everything
beforehand).
The code in the snippet should actually just produce a failure because
of different contents that are compared, but throws this on the aws
instance:
[TypeError] Argument 1 passed to Test\Namespace1\MyTest::getMyResult()
must be an instance of Test\Namespace1\object, instance of stdClass
given, called in /project/tests/Test/Namespace1/MyTest.php on line 14
(The line number might differ)
I hope someone might have an idea how this can fit together and guide me
in the right direction. Happy to assist with further input. Good night!
Test script:
---------------
<?php
namespace Test\Namespace1;
use Codeception\TestCase\Test;
use stdClass;
class MyTest extends Test
{
public function testObjectAnnotation()
{
$a = new stdClass();
$a->c = "1";
$this->assertEquals('anything', $this->getMyResult($a));
}
private function getMyResult(object $anything): string
{
return $anything->c;
}
}
Expected result:
----------------
MyTest: Object annotation
Test tests/Test/Namespace1/MyTest.php:testObjectAnnotation
Failed asserting that two strings are equal.
- Expected | + Actual
@@ @@
-'anything'
+'1'
#1 /project/tests/Test/Namespace1/MyTest.php:14
Actual result:
--------------
[TypeError] Argument 1 passed to Test\Namespace1\MyTest::getMyResult()
must be an instance of Test\Namespace1\object, instance of stdClass
given, called in /project/tests/Test/Namespace1/MyTest.php on line 14
--
Edit bug report at https://bugs.php.net/bug.php?id=79440&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=79440&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=79440&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=79440&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=79440&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=79440&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=79440&r=support
Expected behavior: https://bugs.php.net/fix.php?id=79440&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=79440&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=79440&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=79440&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=79440&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=79440&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=79440&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=79440&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=79440&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=79440&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=79440&r=mysqlcfg