Problem/Motivation

According to the documentation of \Drupal\Core\Entity\EntityTypeInterface::getHandlerClass() the method should return NULL if a handler class is not set.

We never test for this outcome.

Proposed resolution

Add a test case to Drupal\Tests\Core\Entity\EntityTypeTest to ensure that a NULL result is possible.

Remaining tasks

User interface changes

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

hchonov created an issue. See original summary.

hchonov’s picture

Sam152’s picture

If a function doesn't return anything it behaves exactly the same as if it returned NULL. See below:

php > $foo = function() { };
php > var_export($foo());
NULL
php > $bar = function() { return NULL; };
php > var_export($foo() === $bar());
true
dawehner’s picture

Category: Bug report » Task

Yeah this is not a bug , maybe a task ... slightly better code?

vijaycs85’s picture

Issue tags: +Novice, +Vienna2017
vijaycs85’s picture

Mile23’s picture

Title: EntityType::getHandlerClass will not return NULL if a handler class is not set, but according to the interface it should » Add test case for EntityType::getHandlerClass returning NULL
Issue summary: View changes
Status: Needs review » Needs work

Rescoping because EntityType::getHandlerClass() seems to return NULL by not returning anything based on hasHandlerClass().

This makes it an easy unit test to add to Drupal\Tests\Core\Entity\EntityTypeTest, because we never test for this outcome.

Ivan Berezhnov’s picture

Issue tags: +CSKyiv18

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Sivaji_Ganesh_Jojodae’s picture

Status: Needs work » Needs review
FileSize
1.43 KB

Not sure if the attached test & code change is what intended. Please review.

hchonov’s picture

Status: Needs review » Needs work
+++ b/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php
@@ -169,6 +169,23 @@ public function testGetHandler() {
+  public function testGetHandlerClassNull() {
...
+    $this->assertNull($entity_type->getHandlerClass('foo'));
+    $this->assertNull($entity_type->getHandlerClass('foo', 'bar'));

Instead of creating a new test we could incorporate these 2 lines in ::testGetHandler().

Sivaji_Ganesh_Jojodae’s picture

Status: Needs work » Needs review
FileSize
1.12 KB

Makes sense. Patch re-rolled to reflect the change.

hchonov’s picture

Status: Needs review » Reviewed & tested by the community

Thank you.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/lib/Drupal/Core/Entity/EntityType.php
@@ -465,6 +465,7 @@ public function getHandlerClass($handler_type, $nested = FALSE) {
+    return NULL;

This is not needed. It is implicit when there is no return. I do not want to encourage a spate of issues adding return NULL everywhere.

Hardik_Patel_12’s picture

function test()
{
return null;
}

function test()
{
return;
}

function test()
{

}

In all cases there for var_dump(test()); will be:

NULL

Kindly review a new patch.

Hardik_Patel_12’s picture

Status: Needs work » Needs review
ravi.shankar’s picture

Status: Needs review » Reviewed & tested by the community

Patch #18 looks good to me, so making it RTBC.

hchonov’s picture

This is not needed. It is implicit when there is no return. I do not want to encourage a spate of issues adding return NULL everywhere.

I understand your worries, however I consider no explicit return a bad practice and I hope that there aren't that many places without explicit returns.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

@hchonov okay I committed #15. I can buy the argument that when you have a return it's best that all returns are explicit.

Committed and pushed b6bf07b8aa to 9.0.x and a1468fef85 to 8.9.x. Thanks!

  • alexpott committed b6bf07b on 9.0.x
    Issue #2910775 by Sivaji, Hardik_Patel_12, hchonov, Mile23: Add test...

  • alexpott committed a1468fe on 8.9.x
    Issue #2910775 by Sivaji, Hardik_Patel_12, hchonov, Mile23: Add test...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.