Skip to content

Commit

Permalink
Removed dependence on PHP_Timer
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Mar 27, 2014
1 parent 595c4bf commit 9eaaccd
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 77 deletions.
14 changes: 7 additions & 7 deletions CodeSniffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1608,7 +1608,7 @@ public function processFile($file, $contents=null)
private function _processFile($file, $contents)
{
if (PHP_CODESNIFFER_VERBOSITY > 0) {
$startTime = time();
$startTime = microtime(true);
echo 'Processing '.basename($file).' ';
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo PHP_EOL;
Expand All @@ -1626,13 +1626,13 @@ private function _processFile($file, $contents)
$phpcsFile->start($contents);

if (PHP_CODESNIFFER_VERBOSITY > 0) {
$timeTaken = (time() - $startTime);
if ($timeTaken === 0) {
echo 'DONE in < 1 second';
} else if ($timeTaken === 1) {
echo 'DONE in 1 second';
$timeTaken = ((microtime(true) - $startTime) * 1000);
if ($timeTaken < 1000) {
$timeTaken = round($timeTaken);
echo "DONE in {$timeTaken}ms";
} else {
echo "DONE in $timeTaken seconds";
$timeTaken = round(($timeTaken / 1000), 2);
echo "DONE in $timeTaken secs";
}

$errors = $phpcsFile->getErrorCount();
Expand Down
9 changes: 3 additions & 6 deletions CodeSniffer/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -703,16 +703,13 @@ public function printErrorReport(
);
}

// Only print PHP_Timer output if no reports were
// Only print timer output if no reports were
// printed to the screen so we don't put additional output
// in something like an XML report. If we are printing to screen,
// the report types would have already worked out who should
// print the timer info.
if ($toScreen === false
&& PHP_CODESNIFFER_INTERACTIVE === false
&& class_exists('PHP_Timer', false) === true
) {
echo PHP_Timer::resourceUsage().PHP_EOL.PHP_EOL;
if ($toScreen === false && PHP_CODESNIFFER_INTERACTIVE === false) {
PHP_CodeSniffer_Reporting::printRunTime();
}

// They should all return the same value, so it
Expand Down
47 changes: 47 additions & 0 deletions CodeSniffer/Reporting.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ class PHP_CodeSniffer_Reporting
*/
public $totalFixable = 0;

/**
* When the PHPCS run started.
*
* @var float
*/
public static $startTime = 0;

/**
* A list of reports that have written partial report output.
*
Expand Down Expand Up @@ -337,5 +344,45 @@ public function prepareFileReport(PHP_CodeSniffer_File $phpcsFile)

}//end prepareFileReport()

/**
* Start recording time for the run.
*
* @return void
*/
public static function startTiming()
{

self::$startTime = microtime(true);

}//end startTiming()


/**
* Print information about the run.
*
* @return void
*/
public static function printRunTime()
{
$time = ((microtime(true) - self::$startTime) * 1000);

if ($time > 60000) {
$mins = floor($time / 60000);
$secs = round((($time % 60000) / 1000), 2);
$time = $mins.' mins';
if ($secs !== 0) {
$time .= ", $secs secs";
}
} else if ($time > 1000) {
$time = round(($time / 1000), 2).' secs';
} else {
$time = round($time).'ms';
}

$mem = round((memory_get_peak_usage(true) / (1024 * 1024)), 2).'Mb';
echo "Time: $time; Memory: $mem".PHP_EOL.PHP_EOL;

}//end printRunTime()


}//end class
7 changes: 2 additions & 5 deletions CodeSniffer/Reports/Full.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,8 @@ public function generate(

echo $cachedData;

if ($toScreen === true
&& PHP_CODESNIFFER_INTERACTIVE === false
&& class_exists('PHP_Timer', false) === true
) {
echo PHP_Timer::resourceUsage().PHP_EOL.PHP_EOL;
if ($toScreen === true && PHP_CODESNIFFER_INTERACTIVE === false) {
PHP_CodeSniffer_Reporting::printRunTime();
}

}//end generate()
Expand Down
7 changes: 2 additions & 5 deletions CodeSniffer/Reports/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,8 @@ public function generate(

echo str_repeat('-', $width).PHP_EOL;

if ($toScreen === true
&& PHP_CODESNIFFER_INTERACTIVE === false
&& class_exists('PHP_Timer', false) === true
) {
echo PHP_Timer::resourceUsage().PHP_EOL.PHP_EOL;
if ($toScreen === true && PHP_CODESNIFFER_INTERACTIVE === false) {
PHP_CodeSniffer_Reporting::printRunTime();
}

}//end generate()
Expand Down
7 changes: 2 additions & 5 deletions CodeSniffer/Reports/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,8 @@ public function generate(

echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL;

if ($toScreen === true
&& PHP_CODESNIFFER_INTERACTIVE === false
&& class_exists('PHP_Timer', false) === true
) {
echo PHP_Timer::resourceUsage().PHP_EOL.PHP_EOL;
if ($toScreen === true && PHP_CODESNIFFER_INTERACTIVE === false) {
PHP_CodeSniffer_Reporting::printRunTime();
}

}//end generate()
Expand Down
7 changes: 2 additions & 5 deletions CodeSniffer/Reports/Summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,8 @@ public function generate(

echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL;

if ($toScreen === true
&& PHP_CODESNIFFER_INTERACTIVE === false
&& class_exists('PHP_Timer', false) === true
) {
echo PHP_Timer::resourceUsage().PHP_EOL.PHP_EOL;
if ($toScreen === true && PHP_CODESNIFFER_INTERACTIVE === false) {
PHP_CodeSniffer_Reporting::printRunTime();
}

}//end generate()
Expand Down
7 changes: 2 additions & 5 deletions CodeSniffer/Reports/VersionControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,8 @@ public function generate(

echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL;

if ($toScreen === true
&& PHP_CODESNIFFER_INTERACTIVE === false
&& class_exists('PHP_Timer', false) === true
) {
echo PHP_Timer::resourceUsage().PHP_EOL.PHP_EOL;
if ($toScreen === true && PHP_CODESNIFFER_INTERACTIVE === false) {
PHP_CodeSniffer_Reporting::printRunTime();
}

}//end generate()
Expand Down
3 changes: 0 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@
"CodeSniffer/Standards/Zend/Sniffs/"
]
},
"suggest": {
"phpunit/php-timer": "dev-master"
},
"require": {
"php": ">=5.1.2",
"ext-tokenizer": "*"
Expand Down
7 changes: 1 addition & 6 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Custom reports can now specify a $recordErrors member var; this previously only worked for built-in reports
-- When set to FALSE, error messages will not be recorded and only totals will be returned
-- This can save significant memory while processing a large code base
- Removed dependence on PHP_Timer
- PHP tokenizer now supports DEFAULT statements opened with a T_SEMICOLON
- The Squiz and PHPCS standards have increased the max padding for statement alignment from 8 to 12
- Squiz EchoedStringsSniff now supports statments without a semicolon, such as PHP embedded in HTML
Expand Down Expand Up @@ -2204,12 +2205,6 @@ http://pear.php.net/dtd/package-2.0.xsd">
<min>1.4.0b1</min>
</pearinstaller>
</required>
<optional>
<package>
<name>PHP_Timer</name>
<channel>pear.phpunit.de</channel>
</package>
</optional>
</dependencies>
<phprelease>
<installconditions>
Expand Down
16 changes: 5 additions & 11 deletions scripts/build-phar.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,12 +504,10 @@ function addCSStub(&$phar)
{
$stub = '#!/usr/bin/env php'."\n";
$stub .= '<?php error_reporting(E_ALL | E_STRICT);';
$stub .= '@include_once "PHP/Timer.php";';
$stub .= 'if (class_exists("PHP_Timer", false) === true) {';
$stub .= ' PHP_Timer::start();';
$stub .= '}';
$stub .= 'include_once "phar://".__FILE__."/CodeSniffer/CLI.php";';
$stub .= 'include_once "phar://".__FILE__."/CodeSniffer/Reporting.php";';
$stub .= 'include_once "phar://".__FILE__."/CodeSniffer.php";';
$stub .= 'PHP_CodeSniffer_Reporting::startTiming();';
$stub .= '$config = PHP_CodeSniffer::getAllConfigData();';
$stub .= '$phpcs = new PHP_CodeSniffer_CLI();';
$stub .= '$phpcs->checkRequirements();';
Expand All @@ -531,15 +529,13 @@ function addCBFStub(&$phar)
{
$stub = '#!/usr/bin/env php'."\n";
$stub .= '<?php error_reporting(E_ALL | E_STRICT);';
$stub .= '@include_once "PHP/Timer.php";';
$stub .= 'if (class_exists("PHP_Timer", false) === true) {';
$stub .= ' PHP_Timer::start();';
$stub .= '}';
$stub .= 'if (defined("PHP_CODESNIFFER_CBF") === false) {';
$stub .= ' define("PHP_CODESNIFFER_CBF", true);';
$stub .= '}';
$stub .= 'include_once "phar://".__FILE__."/CodeSniffer/CLI.php";';
$stub .= 'include_once "phar://".__FILE__."/CodeSniffer/Reporting.php";';
$stub .= 'include_once "phar://".__FILE__."/CodeSniffer.php";';
$stub .= 'PHP_CodeSniffer_Reporting::startTiming();';
$stub .= '$config = PHP_CodeSniffer::getAllConfigData();';
$stub .= '$phpcs = new PHP_CodeSniffer_CLI();';
$stub .= '$phpcs->checkRequirements();';
Expand Down Expand Up @@ -601,9 +597,7 @@ function addCBFStub(&$phar)
$stub .= ' $exit = 2;';
$stub .= ' }';
$stub .= '}';
$stub .= 'if (class_exists("PHP_Timer", false) === true) {';
$stub .= ' echo PHP_Timer::resourceUsage().PHP_EOL.PHP_EOL;';
$stub .= '}';
$stub .= 'PHP_CodeSniffer_Reporting::printRunTime();';
$stub .= '__HALT_COMPILER();';
$phar->setStub($stub);

Expand Down
14 changes: 5 additions & 9 deletions scripts/phpcbf
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,20 @@

error_reporting(E_ALL | E_STRICT);

// Optionally use PHP_Timer to print time/memory stats for the run.
@include_once 'PHP/Timer.php';
if (class_exists('PHP_Timer', false) === true) {
PHP_Timer::start();
}

if (defined('PHP_CODESNIFFER_CBF') === false) {
define('PHP_CODESNIFFER_CBF', true);
}

if (is_file(dirname(__FILE__).'/../CodeSniffer/CLI.php') === true) {
include_once dirname(__FILE__).'/../CodeSniffer/CLI.php';
include_once dirname(__FILE__).'/../CodeSniffer/Reporting.php';
} else {
include_once 'PHP/CodeSniffer/CLI.php';
include_once 'PHP/CodeSniffer/Reporting.php';
}

PHP_CodeSniffer_Reporting::startTiming();

$phpcs = new PHP_CodeSniffer_CLI();
$phpcs->checkRequirements();
$phpcs->dieOnUnknownArg = false;
Expand Down Expand Up @@ -111,8 +109,6 @@ if ($suffix === '' && $allowPatch === true) {
}
}//end if

if (class_exists('PHP_Timer', false) === true) {
echo PHP_Timer::resourceUsage().PHP_EOL.PHP_EOL;
}
PHP_CodeSniffer_Reporting::printRunTime();

exit($exit);
14 changes: 4 additions & 10 deletions scripts/phpcs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@

error_reporting(E_ALL | E_STRICT);

// Optionally use PHP_Timer to print time/memory stats for the run.
// Note that the reports are the ones who actually print the data
// as they decide if it is ok to print this data to screen.
@include_once 'PHP/Timer.php';
if (class_exists('PHP_Timer', false) === true) {
PHP_Timer::start();
}

if (is_file(dirname(__FILE__).'/../CodeSniffer/CLI.php') === true) {
include_once dirname(__FILE__).'/../CodeSniffer/CLI.php';
include_once dirname(__FILE__).'/../CodeSniffer/Reporting.php';
} else {
include_once 'PHP/CodeSniffer/CLI.php';
include_once 'PHP/CodeSniffer/Reporting.php';
}

PHP_CodeSniffer_Reporting::startTiming();

$phpcs = new PHP_CodeSniffer_CLI();
$phpcs->checkRequirements();

Expand All @@ -40,5 +36,3 @@ if ($numErrors === 0) {
} else {
exit(1);
}

?>

0 comments on commit 9eaaccd

Please sign in to comment.