Skip to content

Instantly share code, notes, and snippets.

@Girgias
Last active March 19, 2019 19:09
Show Gist options
  • Save Girgias/dfd0c323cee2d9a8d619b0253f4df694 to your computer and use it in GitHub Desktop.
Save Girgias/dfd0c323cee2d9a8d619b0253f4df694 to your computer and use it in GitHub Desktop.
PHPDoc-Entity-Counter version 0.1.1
<?php
/*
* TODO
* CLI options:
* - Language (default EN, Active Lang, All)
* - Output format
*
* Optional HTML output
*
* Help section
*/
function getEntityNames(string $fileContent) {
// Retrieve all entity names
$rawEntities = explode('<!ENTITY', $fileContent);
array_shift($rawEntities);
$entityNames = [];
foreach ($rawEntities as $entity) {
$entity = trim($entity);
$entityParts = preg_split('/\s+/', $entity);
$entityName = $entityParts[0];
$entityNames[] = trim($entityName);
}
return $entityNames;
}
const VERSION = '0.1.1';
const BUFFER_LENGTH = 128;
/** CLI Options constants. */
const CLI_OPT_ARG_REQUIRED = ':';
const OPT_OUTPUT_SHORT = 'o';
const OPT_OUTPUT_LONG = 'output';
const OPT_LANGUAGE_SHORT = 'l';
const OPT_LANGUAGE_LONG = 'language';
const OPT_NUMBER_OCCURRENCES_SHORT = 'n';
const OPT_NUMBER_OCCURRENCES_LONG = 'number';
const OPT_FILES_SHORT = 'f';
const OPT_FILES_LONG = 'files';
const OPT_HELP_SHORT = 'h';
const OPT_HELP_LONG = 'help';
const CLI_OPTIONS_SHORT = '' .
OPT_LANGUAGE_SHORT . CLI_OPT_ARG_REQUIRED .
OPT_OUTPUT_SHORT . CLI_OPT_ARG_REQUIRED .
OPT_NUMBER_OCCURRENCES_SHORT . CLI_OPT_ARG_REQUIRED .
OPT_FILES_SHORT .
OPT_HELP_SHORT;
const CLI_OPTIONS_LONG = [
OPT_OUTPUT_LONG . CLI_OPT_ARG_REQUIRED,
OPT_LANGUAGE_LONG . CLI_OPT_ARG_REQUIRED,
OPT_NUMBER_OCCURRENCES_LONG . CLI_OPT_ARG_REQUIRED,
OPT_FILES_LONG,
OPT_HELP_LONG,
];
/**
* Failure codes
*/
const SUCCESS = 0;
const FAILURE = 1;
const LANGUAGE_NON_EXISTENT = 2;
const ENTITY_FILE_NON_EXISTENT = 4;
const OUTPUT_FILE_CREATION_FAILURE = 8;
/* Default options */
$FILES = false;
$NUMBER = 10;
// Currently not editable.
$DOC_FOLDER = './doc-fr';
$LANGUAGE = 'en';
$OUTPUT_TYPE = 'FILE';
$OUTPUT_DEST = getcwd() . 'occurrences-counter-test.txt';
$BUFFER_CALLBACK = null;
$options = getopt(CLI_OPTIONS_SHORT, CLI_OPTIONS_LONG);
// If help invoked exit immediately after displaying the help message
if (array_key_exists(OPT_HELP_SHORT, $options) || array_key_exists(OPT_HELP_LONG, $options)) {
echo 'Help message.' . PHP_EOL;
exit();
}
// Start Performance tracking
$timeStart = microtime(true);
if (array_key_exists(OPT_FILES_SHORT, $options) || array_key_exists(OPT_FILES_LONG, $options)) {
$FILES = true;
}
if (array_key_exists(OPT_NUMBER_OCCURRENCES_SHORT, $options)) {
$NUMBER = (int) $options[OPT_NUMBER_OCCURRENCES_SHORT];
} elseif (array_key_exists(OPT_NUMBER_OCCURRENCES_LONG, $options)) {
$NUMBER = (int) $options[OPT_NUMBER_OCCURRENCES_LONG];
}
if ($OUTPUT_TYPE === 'FILE') {
try {
$outputFile = fopen($OUTPUT_DEST, 'w');
} catch (Error $e) {
exit(OUTPUT_FILE_CREATION_FAILURE);
}
$BUFFER_CALLBACK = function (string $bufferString) use ($outputFile): string {
fwrite($outputFile, $bufferString);
echo $bufferString;
return $bufferString;
};
}
$filePath = $DOC_FOLDER . DIRECTORY_SEPARATOR . $LANGUAGE . DIRECTORY_SEPARATOR . 'language-snippets.ent';
$fileContent = @file_get_contents($filePath);
if ($fileContent === false) {
echo 'Entity file (' . $filePath . ') does not exist.' . PHP_EOL;
exit(ENTITY_FILE_NON_EXISTENT);
}
ob_start($BUFFER_CALLBACK, BUFFER_LENGTH);
echo 'PHPDoc-Entity-Counter version ' . VERSION . PHP_EOL;
echo 'Written by George Peter `Girgias` Banyard' . PHP_EOL;
echo 'Current time: ' . date('Y-m-d H:i:s') . PHP_EOL;
echo 'Working on documentation folder: ' . $DOC_FOLDER . PHP_EOL;
echo 'Working on SVN Revision: ' . shell_exec('svnversion ' . $DOC_FOLDER);
echo 'Working with language: ' . $LANGUAGE . PHP_EOL;
$entityNames = getEntityNames($fileContent);
$nbEntities = count($entityNames);
echo 'Number of entities: ' . $nbEntities . PHP_EOL;
foreach ($entityNames as $name) {
$rawOutput = shell_exec(
'grep -r --exclude-dir .svn "&' . $name . ';" ' . $DOC_FOLDER . DIRECTORY_SEPARATOR . $LANGUAGE
);
if ($rawOutput === null) {
echo $name . ' has no occurrences.' . PHP_EOL;
continue;
}
// trim to remove new line at the end of the output
$outputParts = explode(PHP_EOL, trim($rawOutput));
$occurrences = count($outputParts);
if ($NUMBER !== -1 && $occurrences > $NUMBER) { continue; }
echo $name . ' has ' . $occurrences . ' occurrences.' . PHP_EOL;
if ($FILES === false) { continue; }
foreach ($outputParts as $part) {
echo ' | ' . $part . PHP_EOL;
}
}
$timeEnd = microtime(true);
$executionTime = ($timeEnd - $timeStart)/60;
echo 'Total Execution Time: ' . $executionTime . ' minutes.' . PHP_EOL;
ob_end_flush();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment