Skip to content

Commit 7571062

Browse files
ENGCOM-2022: Handle type errors for objects creation #15895
- Merge Pull Request #15895 from Atwix/magento2:handle-type-error-for-object-manager - Merged commits: 1. f87e101 2. cd3ad3b 3. 85e6dee 4. 8eee7f9 5. 94cb288 6. 599e337 7. e88de64 8. 7be4327 9. 2424032 10. d44148c 11. d05317c 12. bb7583a
2 parents ce5eacd + bb7583a commit 7571062

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

dev/tests/integration/testsuite/Magento/Framework/ObjectManager/ObjectManagerTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
class ObjectManagerTest extends \PHPUnit\Framework\TestCase
99
{
10+
/**#@+
11+
* Test class with type error
12+
*/
13+
const TEST_CLASS_WITH_TYPE_ERROR = \Magento\Framework\ObjectManager\TestAsset\ConstructorWithTypeError::class;
14+
1015
/**#@+
1116
* Test classes for basic instantiation
1217
*/
@@ -138,4 +143,17 @@ public function testNewInstance($actualClassName, array $properties = [], $expec
138143
}
139144
}
140145
}
146+
147+
/**
148+
* Test creating an object and passing incorrect type of arguments to the constructor.
149+
*
150+
* @expectedException \Magento\Framework\Exception\RuntimeException
151+
* @expectedExceptionMessage Error occurred when creating object
152+
*/
153+
public function testNewInstanceWithTypeError()
154+
{
155+
self::$_objectManager->create(self::TEST_CLASS_WITH_TYPE_ERROR, [
156+
'testArgument' => new \stdClass()
157+
]);
158+
}
141159
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\ObjectManager\TestAsset;
9+
10+
/**
11+
* Test asset used to test invalid argument types on the constructor invocation.
12+
*/
13+
class ConstructorWithTypeError
14+
{
15+
/**
16+
* @var Basic
17+
*/
18+
private $testArgument;
19+
20+
/**
21+
* @param Basic $testArgument
22+
*/
23+
public function __construct(Basic $testArgument)
24+
{
25+
$this->testArgument = $testArgument;
26+
}
27+
}

lib/internal/Magento/Framework/ObjectManager/Factory/AbstractFactory.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
*/
66
namespace Magento\Framework\ObjectManager\Factory;
77

8+
use Magento\Framework\Exception\RuntimeException;
89
use Magento\Framework\ObjectManagerInterface;
10+
use Magento\Framework\Phrase;
11+
use Psr\Log\LoggerInterface;
12+
use Magento\Framework\App\ObjectManager;
913

1014
abstract class AbstractFactory implements \Magento\Framework\ObjectManager\FactoryInterface
1115
{
@@ -104,11 +108,23 @@ public function getDefinitions()
104108
* @param array $args
105109
*
106110
* @return object
107-
*
111+
* @throws RuntimeException
108112
*/
109113
protected function createObject($type, $args)
110114
{
111-
return new $type(...array_values($args));
115+
try {
116+
return new $type(...array_values($args));
117+
} catch (\TypeError $exception) {
118+
/** @var LoggerInterface $logger */
119+
$logger = ObjectManager::getInstance()->get(LoggerInterface::class);
120+
$logger->critical(
121+
sprintf('Type Error occurred when creating object: %s, %s', $type, $exception->getMessage())
122+
);
123+
124+
throw new RuntimeException(
125+
new Phrase('Type Error occurred when creating object: %type', ['type' => $type])
126+
);
127+
}
112128
}
113129

114130
/**

0 commit comments

Comments
 (0)