Replace Drupal's module_exists() with PHP's function_exists().

An example:
Instead of
if (module_exists('devel')) {..
use
if (function_exists('dpm')) {..

The advantage is that if the used module's API function changes, it won't break our module. Furthermore the performance of the PHP function is superior.

Disadvantage is that it is harder to debug if the used module's API function changed. It doesn't provoke an error.

Comments

joshi.rohit100’s picture

Status: Active » Needs review
StatusFileSize
new39.64 KB
lolandese’s picture

Status: Needs review » Needs work

Hi,

Thanks for giving me a hand here but .. it takes a bit more than a bulk Find and Replace of module_exists(). See the provided example in the issue description.

Look for a module API function (e.g. dpm) inside the code that is executed by the conditional and use that instead of the module short name (e.g. devel). If you can't find any, that instance is likely better of with the module_exists() and can be left unchanged.

joshi.rohit100’s picture

Status: Needs work » Needs review
StatusFileSize
new8.41 KB

@lolandese Sorry I just partially read the IS ): . In this patch, I have updated the module_exists with function_exists for api funtions. In some cases, multiple functions of same module is being used (ex. taxonomy). Those calls I haven't changed.

lolandese’s picture

Status: Needs review » Needs work

That looks better.

In some cases, multiple functions of same module are being used (ex. taxonomy). Those calls I haven't changed.

Makes sense, but still using a function would give use the performance benefit while still partially protecting against a module's API change. Just take the function you have the impression is more likely to change. For example, between taxonomy_vocabulary_save and taxonomy_vocabulary_machine_name_load I would choose the last one as it seems to be a less generic function compared with the other.

I know this is hypothetical and a subjective but it's still better to make a choice based on something. After these changes I think this is ready for test and commit.

joshi.rohit100’s picture

Status: Needs work » Needs review
StatusFileSize
new9.62 KB
new1.14 KB

Done as per #4

lolandese’s picture

Status: Needs review » Fixed

Applies cleanly:

martin@martin-X501A1:~/www/purple/sites/all/modules/flickr$ git apply -v 2494025-replace-module_exists-5.patch
Checking patch block/flickr_block.install...
Checking patch block/flickr_block.module...
Checking patch cachewarmer/flickrcachewarmer.module...
Checking patch flickr.inc...
Applied patch block/flickr_block.install cleanly.
Applied patch block/flickr_block.module cleanly.
Applied patch cachewarmer/flickrcachewarmer.module cleanly.
Applied patch flickr.inc cleanly.

Functional tested by disabling and enabling the Devel module. "Manually" checked the rest.

Looks good to me.

Thanks for your contribution.

Status: Fixed » Closed (fixed)

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