/** * @param $type * @return array */ public function getPluginIds($type) { $pluginsDirs = false; $config = code_review::getConfig(); switch ($type) { case self::T_PLUGINS_INACTIVE: $pluginsDirs = $this->getPluginIds(self::T_PLUGINS_ALL); $actives = call_user_func($config['plugins_getter'], 'active'); foreach ($actives as $plugin) { if ($plugin instanceof ElggPlugin) { $pluginsDirs = array_diff($pluginsDirs, array($plugin->getID())); } else { $pluginsDirs = array_diff($pluginsDirs, array($plugin)); } } break; case self::T_PLUGINS_ACTIVE: $pluginsDirs = call_user_func($config['plugins_getter'], 'active'); foreach ($pluginsDirs as $key => $plugin) { if ($plugin instanceof ElggPlugin) { $pluginsDirs[$key] = $plugin->getID(); } } break; case self::T_PLUGINS_ALL: $pluginsDirs = code_review::getPluginDirsInDir($config['pluginspath']); break; } return $pluginsDirs; }
public function testGetPrivateFunctionsList() { $functions = code_review::getPrivateFunctionsList(); $this->assertArrayNotHasKey('dummy_deprecated_function1', $functions); /* * foobar_private_api */ $this->assertArrayHasKey('foobar_private_api', $functions); $privateFunction = $functions['foobar_private_api']; $this->assertInstanceOf('CodeReview_Issues_Private', $privateFunction); $this->assertInstanceOf('ArrayAccess', $privateFunction); $this->assertFalse(is_array($privateFunction)); $this->assertArrayHasKey('name', $privateFunction); $this->assertArrayHasKey('file', $privateFunction); $this->assertArrayHasKey('line', $privateFunction); $this->assertArrayHasKey('reason', $privateFunction); $this->assertEquals('foobar_private_api', $privateFunction['name']); $this->assertEquals('private', $privateFunction['reason']); $this->assertEquals("Line " . $privateFunction['line'] . ":\tFunction call: " . $privateFunction['name'] . " (use of function marked private is unsafe)", (string) $privateFunction); /* * foobar_undocumented */ $this->assertArrayHasKey('foobar_undocumented', $functions); $undocumentedFunction = $functions['foobar_undocumented']; $this->assertInstanceOf('CodeReview_Issues_NotDocumented', $undocumentedFunction); $this->assertInstanceOf('ArrayAccess', $undocumentedFunction); $this->assertFalse(is_array($undocumentedFunction)); $this->assertArrayHasKey('name', $undocumentedFunction); $this->assertArrayHasKey('file', $undocumentedFunction); $this->assertArrayHasKey('line', $undocumentedFunction); $this->assertArrayHasKey('reason', $undocumentedFunction); $this->assertEquals('foobar_undocumented', $undocumentedFunction['name']); $this->assertEquals('not_documented', $undocumentedFunction['reason']); $this->assertEquals("Line " . $undocumentedFunction['line'] . ":\tFunction call: " . $undocumentedFunction['name'] . " (use of undocumented core function is unsafe)", (string) $undocumentedFunction); }
/** * Tests normal operation * * @requires PHP 5.3 */ public function testIteratingOverFilesFilteringIncactive() { $path = dirname(__FILE__) . '/test_files/fake_elgg/'; require_once $path . 'engine/start.php'; \code_review::initConfig(array('path' => $path, 'engine_path' => $path . 'engine/', 'pluginspath' => $path . 'mod/', 'plugins_getter' => array($this, 'mocked_plugins_getter'))); $paths = array(array(dirname(__FILE__) . '/test_files/fake_elgg/', true), array(dirname(__FILE__) . '/test_files/fake_elgg' . DIRECTORY_SEPARATOR, true), array(dirname(__FILE__) . '/test_files/fake_elgg', true)); foreach ($paths as $row) { list($path, $skipInactive) = $row; $config = new \CodeReview\Config(array('includeDisabledPlugins' => !$skipInactive)); $baseFileInfo = new \SplFileInfo($path); $i = new \RecursiveDirectoryIterator($path); $i = new \RecursiveIteratorIterator($i, \RecursiveIteratorIterator::LEAVES_ONLY); $i = new \CodeReview\FileFilterIterator($i, $path, $config); $filesFound = array(); /** @var $file \SplFileInfo */ foreach ($i as $file) { $this->assertInstanceOf('\\SplFileInfo', $file); $this->assertNotEquals('.dummy_config', $file->getBasename()); $entry = substr($file->getRealPath(), strlen($path)); if ($entry) { $entry = trim(str_replace('\\', '/', $entry), '/'); $filesFound[] = $entry; } else { //we allow only root dir as exception $this->assertEquals($baseFileInfo->getInode(), $file->getInode()); } } $expected = array('engine/lib/deprecated-1.2.php', 'not_filtered_file', 'mod/ugly_plugin/start.php', 'mod/ugly_plugin/pages/page17.php', 'mod/ugly_plugin/manifest.xml'); $missingFiles = array_diff($expected, $filesFound); $this->assertEquals($missingFiles, array(), "Missing expected files: " . print_r($missingFiles, true)); $unexpected = array('.dummy_config', 'mod/inactive_plugin/start.php', 'mod/inactive_plugin/manifest.xml', 'vendor/unwanted_file', 'vendors/unwanted_file'); $unwantedFiles = array_intersect($unexpected, $filesFound); $this->assertEquals($unwantedFiles, array(), "Got some unwanted files: " . print_r($unwantedFiles, true)); } }
public function testAnalyzerFailOnBadPath() { $config = new \CodeReview\Config(array(), array($this, 'getLatestVersion')); $config->parseInput(array('subpath' => 'does/not/exist', 'version' => '1.2', 'include_disabled_plugins' => false, 'find_deprecated_functions' => true, 'find_private_functions' => false, 'fix_problems' => false)); $generalConfig = \code_review::getConfig(); $this->assertEquals(dirname(__FILE__) . '/test_files/fake_elgg/', $generalConfig['path']); $analyzer = new \CodeReview\Analyzer($config); $this->setExpectedException('CodeReview\\IOException', "Invalid subPath specified. " . dirname(__FILE__) . "/test_files/fake_elgg/does/not/exist/ does not exists!"); $analyzer->analyze(); }
/** * @param array $options * * @todo Move into CodeReviewConfig instead */ public static function initConfig(array $options) { self::$config = $options; $names = array('T_NAMESPACE', 'T_NS_C', 'T_NS_SEPARATOR'); foreach ($names as $name) { if (!defined($name)) { // just define it with value unused by tokenizer to avoid errors on old PHP versions define($name, 10000); } } }
<?php echo elgg_view('code_review/navigation'); //fetch all $functions = code_review::getPrivateFunctionsList(); $fixes = new \CodeReview\CodeFixer(); $title = elgg_echo('code_review:private_list:title'); $body = "<table class=\"elgg-table-alt\">"; $body .= "<tr>" . "<th><strong>" . elgg_echo('code_review:private_list:name') . "</strong></th>" . "<th><strong>" . elgg_echo('code_review:private_list:reason') . "</strong></th>" . "</tr>"; ksort($functions, SORT_STRING); foreach ($functions as $name => $data) { $fileLine = elgg_echo('code_review:private_list:file_line', array($data['file'], $data['line'])); $body .= "<tr><td><abbr title=\"{$fileLine}\">" . $data['name'] . "</abbr></td>" . "<td>" . elgg_echo('code_review:private_list:reason:' . $data['reason']) . "</td></tr>"; } $body .= '</table>'; echo elgg_view_module('featured', $title, $body, array('class' => 'mbl'));
<?php /** * Elgg 1.8 compatibility */ if (!function_exists('elgg_get_version')) { function elgg_get_version($human_readable = false) { global $CONFIG; static $version, $release; if (isset($CONFIG->path)) { if (!isset($version) || !isset($release)) { if (!(include $CONFIG->path . "version.php")) { return false; } } return $human_readable ? $release : $version; } return false; } } code_review::boot(); elgg_register_event_handler('init', 'system', array('code_review', 'init'));
/** * @return string */ private function ouptutUnusedFunctionsReport() { //prepare unused functions report $functions = get_defined_functions(); $functions = array_filter($functions['user'], 'strtolower'); $calledFunctions = array_filter($this->calledFunctions, 'strtolower'); $deprecatedFunctions = array_filter(array_keys(code_review::getDeprecatedFunctionsList($this->maxVersion)), 'strtolower'); $functions = array_diff($functions, $calledFunctions, $deprecatedFunctions); foreach ($functions as $key => $function) { if (function_exists($function)) { $reflectionFunction = new ReflectionFunction($function); if (!$reflectionFunction->isInternal()) { continue; } unset($reflectionFunction); } unset($functions[$key]); } sort($functions); //unused functions report $result = "Not called but defined funcions:\n"; $baseLenght = strlen(elgg_get_root_path()); foreach (array_values($functions) as $functionName) { $reflectionFunction = new ReflectionFunction($functionName); $path = substr($reflectionFunction->getFileName(), $baseLenght); if (strpos($path, 'engine') !== 0) { continue; } $result .= "{$functionName} \t{$path}:{$reflectionFunction->getStartLine()}\n"; } return $result; }
if (preg_match('#^([0-9]+\\.[0-9]+)#', $bigVersion, $matches)) { $bigVersion = $matches[1]; } $subpath = get_input('subpath'); $version = get_input('version', elgg_extract('version', $vars, $bigVersion)); $include_disabled_plugins = get_input('include_disabled_plugins', 0); $find_deprecated_functions = get_input('find_deprecated_functions', 1); $find_private_functions = get_input('find_private_functions', 1); $fix_problems = get_input('fix_problems', 0); echo '<p>'; echo '<label>' . elgg_echo('code_review:subpath') . '</label> '; echo elgg_view('input/text', array('name' => 'subpath', 'value' => $subpath, 'placeholder' => elgg_echo('code_review:subpath:placeholder'))); echo '</p>'; echo '<p>'; echo '<label>' . elgg_echo('code_review:version') . '</label> '; echo elgg_view('input/dropdown', array('name' => 'version', 'value' => $version, 'options' => code_review::getVersionsList())); echo '</p>'; echo '<p>'; echo '<label>' . elgg_echo('code_review:disabled_plugins_only') . '</label> '; echo elgg_view('input/dropdown', array('name' => 'include_disabled_plugins', 'value' => $include_disabled_plugins, 'options_values' => array(0 => elgg_echo('option:no'), 1 => elgg_echo('option:yes')))); echo '</p>'; echo '<p>'; echo '<label>' . elgg_echo('code_review:find_deprecated_functions') . '</label> '; echo elgg_view('input/dropdown', array('name' => 'find_deprecated_functions', 'value' => $find_deprecated_functions, 'options_values' => array(0 => elgg_echo('option:no'), 1 => elgg_echo('option:yes')))); echo '</p>'; echo '<p>'; echo '<label>' . elgg_echo('code_review:find_private_functions') . '</label> '; echo elgg_view('input/dropdown', array('name' => 'find_private_functions', 'value' => $find_private_functions, 'options_values' => array(0 => elgg_echo('option:no'), 1 => elgg_echo('option:yes')))); echo '</p>'; echo '<p>'; echo '<label>' . elgg_echo('code_review:fix_problems') . '</label> ';
<?php echo elgg_view('code_review/navigation'); //fetch all $functions = code_review::getDeprecatedFunctionsList(''); //group by versions $groups = array(); foreach ($functions as $name => $data) { $version = isset($data['version']) ? $data['version'] : 'Unknown'; if (!isset($groups[$version])) { $groups[$version] = array(); } $groups[$version][$name] = $data; } uksort($groups, 'version_compare'); $groups = array_reverse($groups, true); $fixes = new \CodeReview\CodeFixer(); $replaces = $fixes->getBasicFunctionRenames(); foreach ($groups as $version => $group) { $title = elgg_echo('code_review:deprecated_list:title', array($version)); $body = "<table class=\"elgg-table-alt\">"; $body .= "<tr>" . "<th><strong>" . elgg_echo('code_review:deprecated_list:name') . "</strong></th>" . "<th><strong>" . elgg_echo('code_review:deprecated_list:remarks') . "</strong></th>" . "<th><strong>" . elgg_echo('code_review:deprecated_list:solution') . "</strong></th>" . "</tr>"; ksort($group, SORT_STRING); foreach ($group as $name => $data) { $fileLine = elgg_echo('code_review:deprecated_list:file_line', array($data['file'], $data['line'])); $body .= "<tr><td><abbr title=\"{$fileLine}\">" . $data['name'] . "</abbr></td>"; $body .= "<td>" . ($data['fixinfoshort'] ? $data['fixinfoshort'] : '') . '</td>'; $solution = ''; if (isset($replaces[$name])) { $solution = elgg_echo('code_review:solution:basic_replace_with', array($replaces[$name])); }
public function testGetVersionsList() { $versions = \code_review::getVersionsList(); $this->assertEquals(array('1.0', '1.2'), $versions); }
public function setUp() { $path = dirname(__FILE__) . '/test_files/fake_elgg/'; require_once $path . 'engine/start.php'; \code_review::initConfig(array('path' => $path, 'engine_path' => $path . 'engine/', 'pluginspath' => $path . 'mod/', 'plugins_getter' => array($this, 'pluginsGetter'))); }