RuleSet.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /*
  3. * This file is part of the PHP CS utility.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\CS;
  11. /**
  12. * Set of rules to be used by fixer.
  13. *
  14. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  15. *
  16. * @internal
  17. */
  18. final class RuleSet implements RuleSetInterface
  19. {
  20. private $setDefinitions = array(
  21. '@PSR1' => array(
  22. 'encoding' => true,
  23. 'short_tag' => true,
  24. ),
  25. '@PSR2' => array(
  26. '@PSR1' => true,
  27. 'braces' => true,
  28. 'elseif' => true,
  29. 'eof_ending' => true,
  30. 'function_call_space' => true,
  31. 'function_declaration' => true,
  32. 'indentation' => true,
  33. 'line_after_namespace' => true,
  34. 'linefeed' => true,
  35. 'lowercase_constants' => true,
  36. 'lowercase_keywords' => true,
  37. 'method_argument_space' => true,
  38. 'multiple_use' => true,
  39. 'parenthesis' => true,
  40. 'php_closing_tag' => true,
  41. 'single_line_after_imports' => true,
  42. 'trailing_spaces' => true,
  43. 'visibility' => true,
  44. ),
  45. '@Symfony' => array(
  46. '@PSR2' => true,
  47. 'alias_functions' => true,
  48. 'blankline_after_open_tag' => true,
  49. 'concat_without_spaces' => true,
  50. 'double_arrow_multiline_whitespaces' => true,
  51. 'duplicate_semicolon' => true,
  52. 'empty_return' => true,
  53. 'extra_empty_lines' => true,
  54. 'include' => true,
  55. 'list_commas' => true,
  56. 'method_separation' => true,
  57. 'multiline_array_trailing_comma' => true,
  58. 'namespace_no_leading_whitespace' => true,
  59. 'new_with_braces' => true,
  60. 'no_blank_lines_after_class_opening' => true,
  61. 'no_empty_lines_after_phpdocs' => true,
  62. 'object_operator' => true,
  63. 'operators_spaces' => true,
  64. 'phpdoc_align' => true,
  65. 'phpdoc_indent' => true,
  66. 'phpdoc_inline_tag' => true,
  67. 'phpdoc_no_access' => true,
  68. 'phpdoc_no_empty_return' => true,
  69. 'phpdoc_no_package' => true,
  70. 'phpdoc_scalar' => true,
  71. 'phpdoc_separation' => true,
  72. 'phpdoc_summary' => true,
  73. 'phpdoc_to_comment' => true,
  74. 'phpdoc_trim' => true,
  75. 'phpdoc_type_to_var' => true,
  76. 'phpdoc_var_without_name' => true,
  77. 'pre_increment' => true,
  78. 'remove_leading_slash_use' => true,
  79. 'remove_lines_between_uses' => true,
  80. 'return' => true,
  81. 'self_accessor' => true,
  82. 'single_array_no_trailing_comma' => true,
  83. 'single_blank_line_before_namespace' => true,
  84. 'single_quote' => true,
  85. 'spaces_before_semicolon' => true,
  86. 'spaces_cast' => true,
  87. 'standardize_not_equal' => true,
  88. 'ternary_spaces' => true,
  89. 'trim_array_spaces' => true,
  90. 'unalign_double_arrow' => true,
  91. 'unalign_equals' => true,
  92. 'unary_operators_spaces' => true,
  93. 'unused_use' => true,
  94. 'whitespacy_lines' => true,
  95. ),
  96. );
  97. /**
  98. * Set that was used to generate group of rules.
  99. *
  100. * The key is name of rule or set, value is bool if the rule/set should be used.
  101. *
  102. * @var array
  103. */
  104. private $set;
  105. /**
  106. * Group of rules generated from input set.
  107. *
  108. * The key is name of rule, value is bool if the rule/set should be used.
  109. * The key must not point to any set.
  110. *
  111. * @var array
  112. */
  113. private $rules;
  114. public function __construct(array $set = array())
  115. {
  116. $this->set = $set;
  117. $this->resolveSet();
  118. }
  119. public static function create(array $set = array())
  120. {
  121. return new self($set);
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function hasRule($rule)
  127. {
  128. return array_key_exists($rule, $this->rules);
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function getRuleConfiguration($rule)
  134. {
  135. if (!$this->hasRule($rule)) {
  136. throw new \UnexpectedValueException(sprintf('Rule "%s" is not in the set.', $rule));
  137. }
  138. if ($this->rules[$rule] === true) {
  139. return;
  140. }
  141. return $this->rules[$rule];
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. public function getRules()
  147. {
  148. return $this->rules;
  149. }
  150. /**
  151. * {@inheritdoc}
  152. */
  153. public function getSetDefinitionNames()
  154. {
  155. return array_keys($this->setDefinitions);
  156. }
  157. /**
  158. * Get definition of set.
  159. *
  160. * @param string $name name of set
  161. *
  162. * @return array
  163. */
  164. private function getSetDefinition($name)
  165. {
  166. if (!isset($this->setDefinitions[$name])) {
  167. throw new \UnexpectedValueException(sprintf('Set "%s" does not exist.', $name));
  168. }
  169. return $this->setDefinitions[$name];
  170. }
  171. /**
  172. * Resolve input set into group of rules.
  173. *
  174. * @return $this
  175. */
  176. private function resolveSet()
  177. {
  178. $rules = $this->set;
  179. $hasSet = null;
  180. // expand sets
  181. do {
  182. $hasSet = false;
  183. $tmpRules = $rules;
  184. $rules = array();
  185. foreach ($tmpRules as $name => $value) {
  186. if (!$hasSet && '@' === $name[0]) {
  187. $hasSet = true;
  188. $set = $this->getSetDefinition($name);
  189. foreach ($set as $nestedName => $nestedValue) {
  190. // if set value is falsy then disable all fixers in set, if not then get value from set item
  191. $rules[$nestedName] = $value ? $nestedValue : false;
  192. }
  193. continue;
  194. }
  195. $rules[$name] = $value;
  196. }
  197. } while ($hasSet);
  198. // filter out all rules that are off
  199. $rules = array_filter($rules);
  200. $this->rules = $rules;
  201. return $this;
  202. }
  203. }