Skip to content

Instantly share code, notes, and snippets.

@colinmollenhour
Last active October 6, 2023 13:26
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save colinmollenhour/937195 to your computer and use it in GitHub Desktop.
Save colinmollenhour/937195 to your computer and use it in GitHub Desktop.
Generate map of factory method arguments to resolved class names for Magento projects
<?php
/* This script generates a mapping of factory methods/parameters to the resulting class
* names in such a way that PhpStorm 6.0.1 can use for autocompletion and chaining.
*
* Example:
* cd [magento root]
* php ~/makePhpstormMap.php > .phpstorm.meta.php
*
*/
// Init framework
require 'app/Mage.php';
Mage::app();
// Factory methods to search for
$methods = array(
'Mage::helper',
'Mage::getModel',
'Mage::getResourceModel',
'Mage::getSingleton',
'Mage::getResourceSingleton',
);
// Path to search for source files
$projectPath = 'app';
$sourceRegex = '/^.+\.(?:php|phtml)$/';
// Collect class names
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($projectPath, FilesystemIterator::FOLLOW_SYMLINKS));
$files = new RegexIterator($iterator, $sourceRegex, RecursiveRegexIterator::GET_MATCH);
$classes = array();
foreach($files as $file) {
$code = file_get_contents($file[0]);
if ($code === FALSE) die("Could not get contents of {$file[0]}\n");
foreach($methods as $method) {
if(preg_match_all('#'.preg_quote($method).'\s*\(\s*[\'"]([a-zA-Z0-9/_]+)[\'"]#', $code, $matches)) {
if(empty($classes[$method])) $classes[$method] = array();
foreach($matches[1] as $token) {
if(isset($classes[$method][$token])) continue;
$className = NULL;
switch ($method) {
case 'Mage::helper':
$className = Mage::getConfig()->getHelperClassName($token);
break;
case 'Mage::getModel':
case 'Mage::getSingleton':
$className = Mage::getConfig()->getModelClassName($token);
break;
case 'Mage::getResourceModel':
case 'Mage::getResourceSingleton':
$className = Mage::getConfig()->getResourceModelClassName($token);
break;
default:
try {
$instance = call_user_func($method, $token);
if($instance)
$className = get_class($instance);
} catch(Exception $e){}
}
if ($className) {
$classes[$method][$token] = $className;
}
}
}
}
}
$map = <<<PHP
<?php
namespace PHPSTORM_META {
/** @noinspection PhpUnusedLocalVariableInspection */
/** @noinspection PhpIllegalArrayKeyTypeInspection */
\$STATIC_METHOD_TYPES = [
PHP;
foreach ($classes as $method => $_map) {
$map .= " \\$method('') => [\n";
foreach ($_map as $arg => $class) {
$map .= " '$arg' instanceof \\$class,\n";
}
$map .= " ],\n";
}
$map .= <<<PHP
];
}
PHP;
echo $map;
@access3000
Copy link

If the script reads a file with no content it just dies, like in my case where this file was empty:

app/code/local/Alanstormdotcom/Developermanual/templates/adminhtml_alanstormdotcom_developermanual_api_singleResource.phtml

I solved the issue by doing the following:

foreach($files as $file) {
//    $code = file_get_contents($file[0]) or die("Could not get contents of {$file[0]}\n");
    $code = file_get_contents($file[0]);
    if (strlen($code) >0) {
        foreach($methods as $method) {
            if(preg_match_all('#'.preg_quote($method).'\s*\(\s*[\'"]([a-zA-Z0-9/_]+)[\'"]#', $code, $matches)) {
                if(empty($classes[$method])) $classes[$method] = array();
                foreach($matches[1] as $token) {
                    if(isset($classes[$method][$token])) continue;
                    $className = NULL;
                    switch ($method) {
                        case 'Mage::helper':
                            $className = Mage::getConfig()->getHelperClassName($token);
                            break;
                        case 'Mage::getModel':
                        case 'Mage::getSingleton':
                            $className = Mage::getConfig()->getModelClassName($token);
                            break;
                        case 'Mage::getResourceModel':
                        case 'Mage::getResourceSingleton':
                            $className = Mage::getConfig()->getResourceModelClassName($token);
                            break;
                        default:
                            try {
                                $instance = call_user_func($method, $token);
                                if($instance)
                                    $className = get_class($instance);
                            } catch(Exception $e){}
                    }
                    if ($className) {
                        $classes[$method][$token] = $className;
                    }
                }
            }
        }
    }
}

@antonmakarenko
Copy link

Potentially the script can be implemented without executing Magento code. For inspiration, take a look at these tools:
https://github.com/magento/magento2/blob/master/dev/tools/migration/factory_names.php
https://github.com/magento/magento2/blob/master/dev/tests/static/testsuite/Legacy/ClassesTest.php

@colinmollenhour
Copy link
Author

@access3000 I just saw your comment after I updated with a similar fix...

@antonmakarenko Thanks for the info, but to get the rewritten classes to work the simple string manipulation isn't enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment