Created
August 17, 2016 15:17
-
-
Save frak/6bf295462e785c434cd16013dde538dd to your computer and use it in GitHub Desktop.
Removes the comment header blocks on new files
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
#!/usr/bin/env php | |
<?php | |
if ($argc !== 2) { | |
die('Usage: cleanup_header_comments.php directory_to_fix'.PHP_EOL); | |
} | |
$workingDir = $argv[1]; | |
if (is_dir($workingDir) === false || is_writable($workingDir) === false) { | |
die($workingDir.' either does not exist or is not writable'); | |
} | |
$naughtyList = []; | |
recurseDirectory(new DirectoryIterator($workingDir), $naughtyList); | |
if (count($naughtyList) > 0) { | |
outputTheNaughtyList($naughtyList); | |
} | |
/** | |
* Recurses the directory structure and calls the replace function if it finds a file. | |
* | |
* @param DirectoryIterator $iterator | |
* @param array $naughtyList | |
*/ | |
function recurseDirectory(DirectoryIterator $iterator, array &$naughtyList) | |
{ | |
foreach ($iterator as $node) { | |
if ($node->isFile()) { | |
removeCommentsFromFile($node, $naughtyList); | |
} elseif ($node->isDir() && $node->isDot() === false) { | |
recurseDirectory(new \DirectoryIterator($node->getPathname()), $naughtyList); | |
} | |
} | |
} | |
/** | |
* Removes the PHP comment block if it is found at the top of the file and increments the counter for the name of the | |
* offending developer. | |
* | |
* @param DirectoryIterator $file | |
* @param array $naughtyList | |
*/ | |
function removeCommentsFromFile(DirectoryIterator $file, array &$naughtyList) | |
{ | |
$fileContents = file_get_contents($file->getPathname()); | |
$matches = []; | |
if (preg_match('/^<\?php.*?(\/\*\*.*?User: (.*?)\s.*?Date.*?Time.*?\*\/)/s', $fileContents, $matches) > 0) { | |
$fileContents = preg_replace('/^<\?php.*?(\/\*\*.*?User.*?Date.*?Time.*?\*\/)/s', '<?php', $fileContents); | |
file_put_contents($file->getPathname(), $fileContents); | |
$who = trim($matches[2]); | |
if (array_key_exists($who, $naughtyList) === false) { | |
$naughtyList[$who] = 0; | |
} | |
++$naughtyList[$who]; | |
} | |
} | |
/** | |
* Writes the list of naughty developers in descending order. | |
* | |
* @param array $naughtyList | |
*/ | |
function outputTheNaughtyList(array $naughtyList) | |
{ | |
arsort($naughtyList); | |
echo PHP_EOL."The Naughty List \xF0\x9F\x98\xA1".PHP_EOL.PHP_EOL; | |
foreach ($naughtyList as $name => $score) { | |
$nameLength = strlen($name); | |
$scoreLength = strlen($score); | |
$paddingLength = 40 - ($nameLength + $scoreLength); | |
$padding = str_repeat(' ', $paddingLength); | |
echo $name.$padding.$score.PHP_EOL; | |
} | |
echo PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment