Skip to content

Instantly share code, notes, and snippets.

@clarkphp
Created October 25, 2017 19:41
Show Gist options
  • Save clarkphp/865b50659c513d486632ad8b7995ea73 to your computer and use it in GitHub Desktop.
Save clarkphp/865b50659c513d486632ad8b7995ea73 to your computer and use it in GitHub Desktop.
<?php
// analyse-codebase.php
// Runs several PHP static analysis tools; assumes *.phar names
// This could be done with a shell/batch script, instead of php.
// If running under Windows, use paths like 'C:\desired\path\to\file'
// If running under *nix, use paths like '/desired/path/to/file'
// If PHP binary is not in your command path, you need to specify path to it.
// const PHP_EXE = '/the/path/to/your/PHP/executable';
const PHP_EXE = 'php';
// the location where the QA tools reside (anywhere you like)
// const QATOOLS = '/a/folder/containing/this/analyse-codebase.php/script';
const QATOOLS = 'C:\\Users\\clark.e\\PHP-QA-Tools';
// the directory (folder) ENCLOSING the code you wish to analyze
// const CODEBASE_ROOT = '/path/to/parent/of/your/source/code/appname';
const CODEBASE_ROOT = 'C:\\Users\\clark.e\\scratchpad\\qa\\demo\\guzzle';
// where to write the analyses OUTPUT files
const OUTPUT_DIR = 'C:\\Users\\clark.e\\scratchpad\\qa\\demo';
// used to give a name to the output files
$appName = 'guzzle';
// this is a folder containing the code to analyze.
// It is APPENDED to CODEBASE_ROOT, allowing for analysis of
// a particular folder within CODEBASE_ROOT), if needed.
$codebaseLocation = '';
// list of subfolders to exclude from counting (can be empty, but you often should exclude some folders)
// PLEASE check this list and edit accordingly, BEFORE running the script.
// Note, for PHPLOC, any directory in the tree with a name in this array is ignored, not just the top-level dirs.
$excludeDirs = [
];
// becomes part of the output file names
$timestamp = date('YmdHis');
$targetPhpVersion = '7.1';
print phpLoc($appName, $timestamp, $excludeDirs, $codebaseLocation) . PHP_EOL;
print copyPasteDetector($appName, $timestamp, $excludeDirs, $codebaseLocation) . PHP_EOL;
print pDepend($appName, $timestamp, $excludeDirs, $codebaseLocation) . PHP_EOL;
print messDetector($appName, $timestamp, $excludeDirs, $codebaseLocation) . PHP_EOL;
//print phpCompatibility($appName, $timestamp, $excludeDirs, $codebaseLocation, $targetPhpVersion) . PHP_EOL;
function phpLoc($appName, $timestamp, $excludeDirs, $codebaseLocation)
{
$tool = 'phploc';
$options = '--log-xml=' . OUTPUT_DIR . "/$appName-$tool-$timestamp.xml"
. ' --log-csv=' . OUTPUT_DIR . "/$appName-$tool-$timestamp.csv"
. ' --exclude ' . implode(' --exclude ', $excludeDirs);
$output = shell_exec('php ' . QATOOLS . "/$tool.phar $options "
. CODEBASE_ROOT . "/$codebaseLocation"
. ' > ' . OUTPUT_DIR . "/$appName-$tool-$timestamp.out");
return $output;
}
function copyPasteDetector($appName, $timestamp, $excludeDirs, $codebaseLocation)
{
$tool = 'phpcpd';
$options = '--log-pmd=' . OUTPUT_DIR . "/$appName-$tool-$timestamp.xml"
. ' --no-ansi'
. ' --no-interaction'
. ' --exclude ' . implode(' --exclude ', $excludeDirs);
$output = shell_exec('php ' . QATOOLS . "/$tool.phar $options "
. CODEBASE_ROOT . "/$codebaseLocation"
. ' > ' . OUTPUT_DIR . "/$appName-$tool-$timestamp.out");
return $output;
}
function pDepend($appName, $timestamp, $excludeDirs, $codebaseLocation)
{
$tool = 'pdepend';
$options = '--dependency-xml=' . OUTPUT_DIR . "/$appName-$tool-$timestamp-dependencies.xml"
. ' --jdepend-chart=' . OUTPUT_DIR . "/$appName-$tool-$timestamp.svg"
. ' --jdepend-xml=' . OUTPUT_DIR . "/$appName-$tool-$timestamp.xml"
. ' --overview-pyramid=' . OUTPUT_DIR . "/$appName-$tool-$timestamp-overview-pyramid.svg"
. ' --summary-xml=' . OUTPUT_DIR . "/$appName-$tool-$timestamp-summary.xml"
. ' --coderank-mode=inheritance'
// how is this used? . ' --coverage-report=' . OUTPUT_DIR . "\\$appName-$tool-$timestamp-coverage.xml"
. ' --ignore=' . implode(',', $excludeDirs);
$output = shell_exec('php ' . QATOOLS . "/$tool.phar $options "
. CODEBASE_ROOT . "/$codebaseLocation"
. ' > ' . OUTPUT_DIR . "/$appName-$tool-$timestamp.out");
return $output;
}
function messDetector($appName, $timestamp, $excludeDirs, $codebaseLocation)
{
$tool = 'phpmd';
$ruleSets = ['cleancode', 'codesize', 'controversial', 'design', 'naming', 'unusedcode'];
foreach ($ruleSets as $ruleSet) {
$options = "text $ruleSet --reportfile "
. OUTPUT_DIR . "/$appName-$tool-$timestamp-$ruleSet.out"
. ' --exclude ' . implode(',', $excludeDirs);
// --strict: also report those nodes with a @SuppressWarnings annotation
shell_exec('php ' . QATOOLS . "/$tool.phar " . CODEBASE_ROOT . "/$codebaseLocation $options");
print $ruleSet . PHP_EOL;
}
}
function phpCompatibility($appName, $timestamp, $excludeDirs, $codebaseLocation, $targetPhpVersion)
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment