PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/php/pear/PHP/PMD/TextUI/Command.php

https://github.com/KaRLsM/SIFO
PHP | 157 lines | 57 code | 14 blank | 86 comment | 6 complexity | 95eb82842c44294d659867956c4f9f80 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of PHP_PMD.
  4. *
  5. * PHP Version 5
  6. *
  7. * Copyright (c) 2009-2011, Manuel Pichler <mapi@phpmd.org>.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * * Neither the name of Manuel Pichler nor the names of his
  23. * contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * @category PHP
  40. * @package PHP_PMD
  41. * @subpackage TextUI
  42. * @author Manuel Pichler <mapi@phpmd.org>
  43. * @copyright 2009-2011 Manuel Pichler. All rights reserved.
  44. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  45. * @version SVN: $Id$
  46. * @link http://phpmd.org
  47. */
  48. require_once 'PHP/PMD.php';
  49. require_once 'PHP/PMD/TextUI/CommandLineOptions.php';
  50. /**
  51. * This class provides a command line interface for PHP_PMD
  52. *
  53. * @category PHP
  54. * @package PHP_PMD
  55. * @subpackage TextUI
  56. * @author Manuel Pichler <mapi@phpmd.org>
  57. * @copyright 2009-2011 Manuel Pichler. All rights reserved.
  58. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  59. * @version Release: 1.1.1
  60. * @link http://phpmd.org
  61. */
  62. class PHP_PMD_TextUI_Command
  63. {
  64. /**
  65. * Exit codes used by the phpmd command line tool.
  66. */
  67. const EXIT_SUCCESS = 0,
  68. EXIT_EXCEPTION = 1,
  69. EXIT_VIOLATION = 2;
  70. /**
  71. * This method creates a PHP_PMD instance and configures this object based
  72. * on the user's input, then it starts the source analysis.
  73. *
  74. * The return value of this method can be used as an exit code. A value
  75. * equal to <b>EXIT_SUCCESS</b> means that no violations or errors were
  76. * found in the analyzed code. Otherwise this method will return a value
  77. * equal to <b>EXIT_VIOLATION</b>.
  78. *
  79. * @param PHP_PMD_TextUI_CommandLineOptions $opts The prepared command line
  80. * arguments.
  81. *
  82. * @return integer
  83. */
  84. public function run(PHP_PMD_TextUI_CommandLineOptions $opts)
  85. {
  86. if ($opts->hasVersion()) {
  87. fwrite(STDOUT, 'PHPMD 1.1.1 by Manuel Pichler' . PHP_EOL);
  88. return self::EXIT_SUCCESS;
  89. }
  90. // Create a report stream
  91. if ($opts->getReportFile() === null) {
  92. $stream = STDOUT;
  93. } else {
  94. $stream = fopen($opts->getReportFile(), 'wb');
  95. }
  96. // Create renderer and configure output
  97. $renderer = $opts->createRenderer();
  98. $renderer->setWriter(new PHP_PMD_Writer_Stream($stream));
  99. // Create a rule set factory
  100. $ruleSetFactory = new PHP_PMD_RuleSetFactory();
  101. $ruleSetFactory->setMinimumPriority($opts->getMinimumPriority());
  102. $phpmd = new PHP_PMD();
  103. $extensions = $opts->getExtensions();
  104. if ($extensions !== null) {
  105. $phpmd->setFileExtensions(explode(',', $extensions));
  106. }
  107. $ignore = $opts->getIgnore();
  108. if ($ignore !== null) {
  109. $phpmd->setIgnorePattern(explode(',', $ignore));
  110. }
  111. $phpmd->processFiles(
  112. $opts->getInputPath(),
  113. $opts->getRuleSets(),
  114. array($renderer),
  115. $ruleSetFactory
  116. );
  117. if ($phpmd->hasViolations()) {
  118. return self::EXIT_VIOLATION;
  119. }
  120. return self::EXIT_SUCCESS;
  121. }
  122. /**
  123. * The main method that can be used by a calling shell script, the return
  124. * value can be used as exit code.
  125. *
  126. * @param array $args The raw command line arguments array.
  127. *
  128. * @return integer
  129. */
  130. public static function main(array $args)
  131. {
  132. try {
  133. $options = new PHP_PMD_TextUI_CommandLineOptions($args);
  134. $command = new PHP_PMD_TextUI_Command();
  135. $exitCode = $command->run($options);
  136. } catch (Exception $e) {
  137. fwrite(STDERR, $e->getMessage());
  138. fwrite(STDERR, PHP_EOL);
  139. $exitCode = self::EXIT_EXCEPTION;
  140. }
  141. return $exitCode;
  142. }
  143. }