Skip to content
This repository has been archived by the owner on Feb 6, 2020. It is now read-only.

ReflectionBasedAbstractFactory should not be expected to instantiate private constructors #268

Conversation

asgrim
Copy link
Contributor

@asgrim asgrim commented May 8, 2018

I noticed that when using the ReflectionBasedAbstractFactory, it chokes when classes with private constructors are used. These types of classes should be ignored from the start, so I've updated canCreate to return false where any class has a private constructor.

@@ -1,7 +1,7 @@
<?php
/**
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Year range: 2016-2018

@@ -135,10 +135,27 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o

/**
* {@inheritDoc}
* @throws \ReflectionException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... I know it could be thrown, but I don't think we should have it here.


/**
* @param string $requestedName
* @return bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely we don't need two above doc-block comments, as these are duplicated with method signature.
The third one, yes, it's possible but as it is not throw explicitly I would remove it as well.

@@ -1,7 +1,7 @@
<?php
/**
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Year range, please.

@@ -17,6 +17,8 @@

class ReflectionBasedAbstractFactoryTest extends TestCase
{
private $container;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add docblock:

/** @var ContainerInterface|ObjectProphecy $container */

@@ -38,6 +40,20 @@ public function testCanCreateReturnsFalseForNonClassRequestedNames($requestedNam
self::assertFalse($factory->canCreate($this->container->reveal(), $requestedName));
}

/**
* @throws \ReflectionException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No point of having it here.

@asgrim asgrim force-pushed the feature/ignore-private-constructors-reflection-abstract-factory branch from 5d1b0aa to 9e33889 Compare May 8, 2018 13:25
@asgrim
Copy link
Contributor Author

asgrim commented May 8, 2018

@webimpress thanks for review! updated

Copy link
Member

@michalbundyra michalbundyra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return true;
}

return $constructor->isPublic();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could these be boiled down to:

return $constructor === null || $constructor->isPublic();

?

@@ -38,6 +42,17 @@ public function testCanCreateReturnsFalseForNonClassRequestedNames($requestedNam
self::assertFalse($factory->canCreate($this->container->reveal(), $requestedName));
}

public function testCanCreateReturnsFalseWhenConstructorIsPrivate() : void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also looks like the new method would return true if no constructor is defined, correct? If so, that deserves its own test case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct; test case added

@GeeH
Copy link
Contributor

GeeH commented Dec 14, 2018

Can this be merged now @weierophinney

@Ocramius Ocramius added this to the 3.3.3 milestone Dec 14, 2018
@Ocramius Ocramius added bug and removed enhancement labels Dec 14, 2018
@Ocramius
Copy link
Member

@GeeH @asgrim we need this to re-target develop, since it is a bug fix

@weierophinney
Copy link
Member

weierophinney commented Dec 14, 2018 via email

@Ocramius
Copy link
Member

ReflectionBasedAbstractFactory currently crashes hard when a ctor is private: I re-labeled this as bug.

@Xerkus Xerkus force-pushed the feature/ignore-private-constructors-reflection-abstract-factory branch from 3026296 to 3378c43 Compare December 20, 2018 08:37
@Xerkus Xerkus changed the base branch from develop to master December 20, 2018 08:39
@Xerkus Xerkus modified the milestones: 3.3.3, 3.4.0 Dec 20, 2018
@Xerkus Xerkus merged commit 8d83c41 into zendframework:master Dec 20, 2018
Xerkus added a commit that referenced this pull request Dec 20, 2018
Xerkus added a commit that referenced this pull request Dec 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants