Last active
January 16, 2017 16:56
-
-
Save wilcorrea/af83a27bf495b4180d24a0dacc6d02be to your computer and use it in GitHub Desktop.
Construct Converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* this file will run a function to read your php files, search for a constructor method and change: | |
* - public function NameOfClass | |
* to | |
* - public function __construct | |
* | |
* usage: php ./construct.php -d=/home/me/php/src -s=warning | |
*/ | |
if (isset($argv[1])) { | |
$parameters = parseParameters($argv); | |
if (isset($parameters['-d'])) { | |
$dir = realpath($parameters['-d']); | |
$show = isset($parameters['-s']) ? $parameters['-s'] : 'all'; | |
convertConstruct($dir, $show); | |
} | |
} | |
/** | |
* @param string $dir | |
* @param string $show | |
* @return null | |
*/ | |
function convertConstruct(string $dir, string $show = 'all') | |
{ | |
if (!is_dir($dir)) { | |
return null; | |
} | |
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::SELF_FIRST); | |
foreach ($files as $file) { | |
if (is_dir($file->getFilename())) { | |
continue; | |
} else { | |
if (strpos($file->getFilename(), '.php') !== false) { | |
constructor($dir, $file->getPathname(), $show); | |
} | |
} | |
} | |
} | |
/** | |
* @param $dir | |
* @param $filename | |
* @param $show | |
*/ | |
function constructor($dir, $filename, $show) | |
{ | |
$lines = explode(PHP_EOL, file_get_contents($filename)); | |
$class = ''; | |
$constructor = false; | |
foreach ($lines as $key => $line) { | |
$_line = trim($line); | |
if (strpos($_line, 'class') === 0) { | |
$peaces = explode(' ', str_replace('{', '', $_line)); | |
if (isset($peaces[1])) { | |
$class = $peaces[1]; | |
} | |
} | |
if ($class && strpos($_line, 'function') !== false) { | |
if (strpos($_line, '__construct') !== false) { | |
$constructor = true; | |
break; | |
} | |
$constructor = strpos($_line, ' ' . $class) !== false; | |
if ($constructor) { | |
$changed = true; | |
$lines[$key] = str_replace(' ' . $class, ' __construct', $lines[$key]); | |
break; | |
} | |
} | |
} | |
if ($class) { | |
$print = $show === 'ok' || $show === 'all'; | |
$relative = str_replace($dir, '', $filename); | |
$message = "[ok]"; | |
if (!$constructor) { | |
$message = "[warning] {$class} do not have constructor"; | |
$print = $show === 'warning' || $show === 'all'; | |
} | |
if ($print) { | |
echo "{$relative}: {$message}\n"; | |
} | |
} | |
if (isset($changed)) { | |
file_put_contents($filename, implode(PHP_EOL, $lines)); | |
} | |
} | |
/** | |
* @param $argv | |
* @return array | |
*/ | |
function parseParameters(array $argv): array | |
{ | |
$parameters = []; | |
foreach ($argv as $arg) { | |
$in = explode("=", $arg); | |
if (count($in) == 2) { | |
$parameters[$in[0]] = $in[1]; | |
} else { | |
$parameters[$in[0]] = 0; | |
} | |
} | |
return $parameters; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment