Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Github #26532: di:setup:compile fails with anonymous classes #26533

Merged
merged 4 commits into from
Feb 9, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 47 additions & 39 deletions setup/src/Magento/Setup/Module/Di/Code/Scanner/PhpScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Setup\Module\Di\Code\Scanner;

use Magento\Framework\Api\Code\Generator\ExtensionAttributesGenerator;
Expand All @@ -12,9 +14,7 @@
use \Magento\Framework\Reflection\TypeProcessor;

/**
* Class PhpScanner
*
* @package Magento\Setup\Module\Di\Code\Scanner
* Finds factory and extension attributes classes which require auto-generation.
*/
class PhpScanner implements ScannerInterface
{
Expand Down Expand Up @@ -53,15 +53,28 @@ public function __construct(Log $log, TypeProcessor $typeProcessor = null)
protected function _findMissingClasses($file, $classReflection, $methodName, $entityType)
{
$missingClasses = [];
if ($classReflection->hasMethod($methodName)) {
$constructor = $classReflection->getMethod($methodName);
$parameters = $constructor->getParameters();
/** @var $parameter \ReflectionParameter */
foreach ($parameters as $parameter) {
preg_match('/\[\s\<\w+?>\s([\w\\\\]+)/s', $parameter->__toString(), $matches);
if (isset($matches[1]) && substr($matches[1], -strlen($entityType)) == $entityType) {
$missingClassName = $matches[1];
if ($this->shouldGenerateClass($missingClassName, $entityType, $file)) {
if (!$classReflection->hasMethod($methodName)) {
return $missingClasses;
}

$factorySuffix = '\\' . ucfirst(FactoryGenerator::ENTITY_TYPE);
joni-jones marked this conversation as resolved.
Show resolved Hide resolved
$constructor = $classReflection->getMethod($methodName);
$parameters = $constructor->getParameters();
/** @var $parameter \ReflectionParameter */
foreach ($parameters as $parameter) {
preg_match('/\[\s\<\w+?>\s([\w\\\\]+)/s', $parameter->__toString(), $matches);
if (isset($matches[1]) && substr($matches[1], -strlen($entityType)) == $entityType) {
$missingClassName = $matches[1];
if ($this->shouldGenerateClass($missingClassName, $entityType, $file)) {

if (substr($missingClassName, -strlen($factorySuffix)) == $factorySuffix) {
$entityName = rtrim(substr($missingClassName, 0, -strlen($factorySuffix)), '\\');
$this->_log->add(
Log::CONFIGURATION_ERROR,
$missingClassName,
'Invalid Factory declaration for class ' . $entityName . ' in file ' . $file
);
} else {
$missingClasses[] = $missingClassName;
}
}
Expand Down Expand Up @@ -110,24 +123,12 @@ protected function getSourceClassName($missingClassName, $entityType)
*/
protected function _fetchFactories($reflectionClass, $file)
{
$factorySuffix = '\\' . ucfirst(FactoryGenerator::ENTITY_TYPE);
$absentFactories = $this->_findMissingClasses(
$file,
$reflectionClass,
'__construct',
ucfirst(FactoryGenerator::ENTITY_TYPE)
);
foreach ($absentFactories as $key => $absentFactory) {
if (substr($absentFactory, -strlen($factorySuffix)) == $factorySuffix) {
$entityName = rtrim(substr($absentFactory, 0, -strlen($factorySuffix)), '\\');
$this->_log->add(
Log::CONFIGURATION_ERROR,
$absentFactory,
'Invalid Factory declaration for class ' . $entityName . ' in file ' . $file
);
unset($absentFactories[$key]);
}
}
return $absentFactories;
}

Expand All @@ -150,21 +151,19 @@ protected function _fetchMissingExtensionAttributesClasses($reflectionClass, $fi
$missingClassName = $returnType['type'];
if ($this->shouldGenerateClass($missingClassName, $entityType, $file)) {
$missingExtensionInterfaces[] = $missingClassName;

$extension = rtrim(substr($missingClassName, 0, -strlen('Interface')), '\\');
if (!class_exists($extension)) {
$missingExtensionInterfaces[] = $extension;
}
$extensionFactory = $extension . 'Factory';
if (!class_exists($extensionFactory)) {
$missingExtensionInterfaces[] = $extensionFactory;
}
}
}
$missingExtensionClasses = [];
$missingExtensionFactories = [];
foreach ($missingExtensionInterfaces as $missingExtensionInterface) {
$extension = rtrim(substr($missingExtensionInterface, 0, -strlen('Interface')), '\\');
if (!class_exists($extension)) {
$missingExtensionClasses[] = $extension;
}
$extensionFactory = $extension . 'Factory';
if (!class_exists($extensionFactory)) {
$missingExtensionFactories[] = $extensionFactory;
}
}
return array_merge($missingExtensionInterfaces, $missingExtensionClasses, $missingExtensionFactories);

return $missingExtensionInterfaces;
}

/**
Expand Down Expand Up @@ -223,8 +222,17 @@ protected function _fetchClasses($namespace, $tokenIterator, $count, $tokens)
{
$classes = [];
for ($tokenOffset = $tokenIterator + 1; $tokenOffset < $count; ++$tokenOffset) {
if ($tokens[$tokenOffset] === '{') {
$classes[] = $namespace . "\\" . $tokens[$tokenIterator + 2][1];
if ($tokens[$tokenOffset] !== '{') {
continue;
}
// anonymous classes should be omitted
if (is_array($tokens[$tokenIterator - 2]) && $tokens[$tokenIterator - 2][0] === T_NEW) {
continue;
}

$class = $namespace . "\\" . $tokens[$tokenIterator + 2][1];
if (!in_array($class, $classes)) {
$classes[] = $class;
}
}
return $classes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Setup\Test\Unit\Module\Di\Code\Reader;

use Magento\Framework\App\Filesystem\DirectoryList;
Expand Down Expand Up @@ -37,6 +39,6 @@ public function testGetList()
$pathToScan = str_replace('\\', '/', realpath(__DIR__ . '/../../') . '/_files/app/code/Magento/SomeModule');
$actual = $this->model->getList($pathToScan);
$this->assertTrue(is_array($actual));
$this->assertCount(5, $actual);
$this->assertCount(6, $actual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,74 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Setup\Test\Unit\Module\Di\Code\Scanner;

require_once __DIR__ . '/../../_files/app/code/Magento/SomeModule/Helper/Test.php';
require_once __DIR__ . '/../../_files/app/code/Magento/SomeModule/ElementFactory.php';
require_once __DIR__ . '/../../_files/app/code/Magento/SomeModule/Model/DoubleColon.php';
require_once __DIR__ . '/../../_files/app/code/Magento/SomeModule/Api/Data/SomeInterface.php';
require_once __DIR__ . '/../../_files/app/code/Magento/SomeModule/Model/StubWithAnonymousClass.php';

use Magento\Framework\Reflection\TypeProcessor;
use Magento\Setup\Module\Di\Code\Scanner\PhpScanner;
use Magento\Setup\Module\Di\Compiler\Log\Log;
use PHPUnit\Framework\MockObject\MockObject;

class PhpScannerTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Setup\Module\Di\Code\Scanner\PhpScanner
* @var PhpScanner
*/
protected $_model;
private $scanner;

/**
* @var string
*/
protected $_testDir;

/**
* @var array
*/
protected $_testFiles = [];
private $testDir;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var Log|MockObject
*/
protected $_logMock;
private $log;

protected function setUp()
{
$this->_logMock = $this->createMock(\Magento\Setup\Module\Di\Compiler\Log\Log::class);
$this->_model = new \Magento\Setup\Module\Di\Code\Scanner\PhpScanner($this->_logMock, new TypeProcessor());
$this->_testDir = str_replace('\\', '/', realpath(__DIR__ . '/../../') . '/_files');
$this->log = $this->createMock(Log::class);
$this->scanner = new PhpScanner($this->log, new TypeProcessor());
$this->testDir = str_replace('\\', '/', realpath(__DIR__ . '/../../') . '/_files');
}

public function testCollectEntities()
{
$this->_testFiles = [
$this->_testDir . '/app/code/Magento/SomeModule/Helper/Test.php',
$this->_testDir . '/app/code/Magento/SomeModule/Model/DoubleColon.php',
$this->_testDir . '/app/code/Magento/SomeModule/Api/Data/SomeInterface.php'
$testFiles = [
$this->testDir . '/app/code/Magento/SomeModule/Helper/Test.php',
$this->testDir . '/app/code/Magento/SomeModule/Model/DoubleColon.php',
$this->testDir . '/app/code/Magento/SomeModule/Api/Data/SomeInterface.php',
$this->testDir . '/app/code/Magento/SomeModule/Model/StubWithAnonymousClass.php',
];

$this->_logMock->expects(
$this->at(0)
)->method(
'add'
)->with(
4,
'Magento\SomeModule\Module\Factory',
'Invalid Factory for nonexistent class Magento\SomeModule\Module in file ' . $this->_testFiles[0]
);
$this->_logMock->expects(
$this->at(1)
)->method(
'add'
)->with(
4,
'Magento\SomeModule\Element\Factory',
'Invalid Factory declaration for class Magento\SomeModule\Element in file ' . $this->_testFiles[0]
);
$this->log->expects(self::at(0))
->method('add')
->with(
4,
'Magento\SomeModule\Module\Factory',
'Invalid Factory for nonexistent class Magento\SomeModule\Module in file ' . $testFiles[0]
);
$this->log->expects(self::at(1))
->method('add')
->with(
4,
'Magento\SomeModule\Element\Factory',
'Invalid Factory declaration for class Magento\SomeModule\Element in file ' . $testFiles[0]
);

$result = $this->scanner->collectEntities($testFiles);

$this->assertEquals(
self::assertEquals(
['\\' . \Magento\Eav\Api\Data\AttributeExtensionInterface::class],
$this->_model->collectEntities($this->_testFiles)
$result
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\SomeModule\Model;

use Magento\SomeModule\DummyFactory;

class StubWithAnonymousClass
{
/**
* @var DummyFactory
*/
private $factory;

public function __construct(DummyFactory $factory)
{
$this->factory = $factory;
}

public function getSerializable(): \JsonSerializable
{
return new class() implements \JsonSerializable {
/**
* @inheritDoc
*/
public function jsonSerialize()
{
return [1, 2, 3];
}
};
}
}