Skip to content

Commit

Permalink
Update xml generation
Browse files Browse the repository at this point in the history
  • Loading branch information
wapmorgan committed May 1, 2018
1 parent 77c0030 commit 4afd355
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,19 @@ Summary report in this case will not be added at the end.
# Help
Full list of available options:
``` sh
```sh
> phpca -h
PhpCodeAnalyzer
Usage:
phpca [-v] [--no-report] [--no-progress] [--since-version=<version>] FILES...
phpca [-v] --extension=<ext> FILES...
phpca [-v] [-q] [--output=<path>] [--no-report] [--no-progress] [--since-version=<version>] FILES...
phpca [-v] [-q] [--output=<path>] --extension=<ext> FILES...
phpca -h
Options:
-h --help Show this text
-v --verbose Show more debug text
-q --quiet Don't print any messages
--output=<path> Path where to generate XML report
--extension=<ext> Look for usage a specific extension
--no-report Turn off summary report
--no-progress Turn off progress
Expand All @@ -98,7 +101,7 @@ The recommended way to install _phpca_ is as phar-package.
chmod +x phpca.phar
sudo mv phpca.phar /usr/local/bin/phpca
```
Further I will use commands for PhpCodeAnalyzer installed as phar or globally with composer, but if you've installed it locally with composer, just replace `phpca` command with `vendor/bin/phpca`.
## Composer
Expand Down
22 changes: 13 additions & 9 deletions src/PhpCodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class PhpCodeAnalyzer {
private $usedExtensions = array();
private $sinceVersion = null;

const XML_ENTRY_TEMPLATE = '<file path="%s" extension="%s" line="%d" type="%s">%s</file>';
const XML_BEGINNING_TEMPLATE = '<?xml version="1.0" encoding="UTF-8"?><php-code-analyzer version="%s"><files>';
const XML_ENDING = '</files></php-code-analyzer>';

public function setSinceVersion($version){
if (!preg_match('~^[[:digit:]]{1}\.[[:digit:]]{1}(\.[[:digit:]]{1})?$~', $version, $match))
return false;
Expand Down Expand Up @@ -286,13 +290,15 @@ protected function catchAsStringUntilOpenCurlyBrace(array $tokens, $pos) {
}

public function printUsedExtensions() {
$this->println('');
$this->println();
if (empty($this->usedExtensions)) {
$this->printMsg('Your code has no usage of non-built-in extension.');
return null;
}

$this->println('Used non-built-in extensions in your code:');
arsort($this->usedExtensions);

foreach ($this->usedExtensions as $extension => $uses_number) {
$extension_data = $this->extensions[$extension];

Expand Down Expand Up @@ -327,7 +333,7 @@ public function printUsedExtensions() {
$this->printMsg(' Only windows is supported by this extension.');
}

$this->println('');
$this->println();
}
}

Expand All @@ -347,15 +353,15 @@ protected function getExtensionFiles()
return $files;
}

private function println($message, $verbose = false)
private function println($message = null, $verbose = false)
{
$this->printMsg($message.PHP_EOL, $verbose);
}

private function printMsg($message, $verbose = false)
{
if ($_ENV['quiet']) {
return '';
return null;
}
if ($_ENV['verbose'] || !$verbose) {
fwrite(STDOUT, $message);
Expand All @@ -367,8 +373,7 @@ private function printXmlFileData($file, $line, $extension, $type, $name)
if ($_ENV['output'] === null) {
return;
}
$pattern = '<file path="%s" extension="%s" line="%d" type="%s">%s</file>';
file_put_contents($_ENV['output'], sprintf($pattern, $file, $extension, $line, $type, $name), FILE_APPEND);
file_put_contents($_ENV['output'], sprintf(self::XML_ENTRY_TEMPLATE, $file, $extension, $line, $type, $name), FILE_APPEND);
}

public function printXmlStart()
Expand All @@ -377,15 +382,14 @@ public function printXmlStart()
return;
}
$version = file_exists(__DIR__.'/../bin/version.txt') ? trim(file_get_contents(__DIR__.'/../bin/version.txt')) : null;
$pattern = '<?xml version="1.0" encoding="UTF-8"?><php-code-analyzer version="%s"><files>';
file_put_contents($_ENV['output'], sprintf($pattern, $version));
file_put_contents($_ENV['output'], sprintf(self::XML_BEGINNING_TEMPLATE, $version));
}

public function printXmlEnd()
{
if ($_ENV['output'] === null) {
return;
}
file_put_contents($_ENV['output'], '</files></php-code-analyzer>', FILE_APPEND);
file_put_contents($_ENV['output'], self::XML_ENDING, FILE_APPEND);
}
}

0 comments on commit 4afd355

Please sign in to comment.