/src/Configuration.php

https://github.com/GunioRobot/phpca · PHP · 444 lines · 163 code · 49 blank · 232 comment · 16 complexity · 3feda4ed4a059c0acc876cad774e219f MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright (c) 2009 Stefan Priebsch <stefan@priebsch.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of Stefan Priebsch nor the names of contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER ORCONTRIBUTORS
  24. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25. * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * @package PHPca
  33. * @author Stefan Priebsch <stefan@priebsch.de>
  34. * @copyright Stefan Priebsch <stefan@priebsch.de>. All rights reserved.
  35. * @license BSD License
  36. */
  37. namespace spriebsch\PHPca;
  38. /**
  39. * The PHPca configuration.
  40. *
  41. * @author Stefan Priebsch <stefan@priebsch.de>
  42. * @copyright Stefan Priebsch <stefan@priebsch.de>. All rights reserved.
  43. */
  44. class Configuration
  45. {
  46. /**
  47. * Global configuration settings.
  48. *
  49. * @var array
  50. */
  51. protected $settings = array();
  52. /**
  53. * Configuration settings for individual rules.
  54. *
  55. * @var array
  56. */
  57. protected $ruleSettings = array();
  58. /**
  59. * The coding standard to enforce.
  60. *
  61. * @var string
  62. */
  63. protected $codingStandard;
  64. /**
  65. * File extensions to analyze.
  66. *
  67. * @var array
  68. */
  69. protected $extensions = array('php');
  70. /**
  71. * The line endings
  72. *
  73. * @var string
  74. */
  75. protected $lineEndings;
  76. /**
  77. * The indentation string
  78. *
  79. * @var string
  80. */
  81. protected $indentation;
  82. /**
  83. * Additional paths to load rules from.
  84. *
  85. * @var array
  86. */
  87. protected $rulePaths = array();
  88. /**
  89. * Rules to enforce.
  90. *
  91. * @var array
  92. */
  93. protected $rules = array();
  94. /**
  95. * Files to skip.
  96. *
  97. * @var array
  98. */
  99. protected $skipFiles = array();
  100. /**
  101. * Constructs the object.
  102. *
  103. * @param string $basePath
  104. * @return null
  105. */
  106. public function __construct($basePath)
  107. {
  108. $this->basePath = realpath($basePath);
  109. }
  110. /**
  111. * Converts paths to absolute paths.
  112. *
  113. * @param string $path Path
  114. * @return string
  115. * @todo fix for Windows
  116. */
  117. protected function toAbsolutePath($path)
  118. {
  119. if (substr($path, 0, 1) == '/') {
  120. return $path;
  121. }
  122. return $this->basePath . '/' . $path;
  123. }
  124. /**
  125. * Returns the base path.
  126. *
  127. * @return null
  128. */
  129. public function getBasePath()
  130. {
  131. return $this->basePath;
  132. }
  133. /**
  134. * Sets the standard settings read from an ini file.
  135. *
  136. * @param array $settings
  137. * @return null
  138. */
  139. public function setStandard(array $settings)
  140. {
  141. if (isset($settings['PHPca'])) {
  142. $this->settings = $settings['PHPca'];
  143. unset($settings['PHPca']);
  144. }
  145. $this->ruleSettings = $settings;
  146. }
  147. /**
  148. * Sets the configuration settings read from an ini file.
  149. *
  150. * @param array $configuration
  151. * @return null
  152. * @todo make global section parsing generic
  153. */
  154. public function setConfiguration(array $configuration)
  155. {
  156. if (isset($configuration['PHPca'])) {
  157. $this->settings = array_replace($this->settings, $configuration['PHPca']);
  158. unset($configuration['PHPca']);
  159. }
  160. if (isset($this->settings['additional_rules'])) {
  161. $paths = explode(',', $this->settings['additional_rules']);
  162. $this->rulePaths = array_map('trim', $paths);
  163. }
  164. if (isset($this->settings['skip'])) {
  165. $files = explode(',', $this->settings['skip']);
  166. $this->skipFiles = array_map('trim', $files);
  167. }
  168. if (isset($this->settings['line_endings'])) {
  169. $this->lineEndings = $this->settings['line_endings'];
  170. }
  171. if (isset($this->settings['indentation'])) {
  172. $this->indentation = $this->settings['indentation'];
  173. }
  174. $this->ruleSettings = array_replace($this->ruleSettings, $configuration);
  175. }
  176. /**
  177. * Sets the name of the coding standard to use.
  178. *
  179. * @param string $codingStandard
  180. * @return null
  181. */
  182. public function setCodingStandard($codingStandard)
  183. {
  184. $this->codingStandard = $codingStandard;
  185. }
  186. /**
  187. * Returns the coding standard used.
  188. *
  189. * @return string
  190. */
  191. public function getCodingStandard()
  192. {
  193. return $this->codingStandard;
  194. }
  195. /**
  196. * Sets the file extensions to analyze.
  197. *
  198. * @param array $extensions Array of file extensions
  199. * @return null
  200. */
  201. public function setExtensions(array $extensions)
  202. {
  203. $this->extensions = $extensions;
  204. }
  205. /**
  206. * Adds a file extensions to analyze.
  207. *
  208. * @param string $extension File extension
  209. * @return null
  210. */
  211. public function addExtension($extension)
  212. {
  213. $this->extensions[] = $extension;
  214. }
  215. /**
  216. * Returns the file extensions to analyze.
  217. *
  218. * @return array
  219. */
  220. public function getExtensions()
  221. {
  222. if (isset($this->settings['extensions'])) {
  223. $extensions = explode(',', $this->settings['extensions']);
  224. return array_map('trim', $extensions);
  225. }
  226. return $this->extensions;
  227. }
  228. /**
  229. * Sets the line endings.
  230. *
  231. * @param string $lineEndings The line endings
  232. * @return null
  233. */
  234. public function setLineEndings($lineEndings)
  235. {
  236. $this->lineEndings = $lineEndings;
  237. }
  238. /**
  239. * Returns the line endings.
  240. *
  241. * @return string
  242. */
  243. public function getLineEndings()
  244. {
  245. return $this->lineEndings;
  246. }
  247. /**
  248. * Sets the indentation string.
  249. *
  250. * @param string $indentation
  251. * @return null
  252. */
  253. public function setIndentation($indentation)
  254. {
  255. $this->indentation = $indentation;
  256. }
  257. /**
  258. * Returns the indentation string.
  259. *
  260. * @return string
  261. */
  262. public function getIndentation()
  263. {
  264. return $this->indentation;
  265. }
  266. /**
  267. * Sets the rule paths to load additional rules from.
  268. *
  269. * @param array $rulePaths Array of rule paths
  270. * @return null
  271. */
  272. public function setRulePaths(array $rulePaths)
  273. {
  274. $this->rulePaths = $rulePaths;
  275. }
  276. /**
  277. * Adds another path to load additional rules from.
  278. *
  279. * @param array $paths Rule paths
  280. * @return null
  281. */
  282. public function addRulePath($path)
  283. {
  284. $this->rulePaths[] = $path;
  285. }
  286. /**
  287. * Returns the additional rule paths from the configuration file.
  288. *
  289. * @return array
  290. */
  291. public function getRulePaths()
  292. {
  293. return $this->rulePaths;
  294. }
  295. /**
  296. * Sets the rules to enforce.
  297. *
  298. * @param array $extensions
  299. * @return null
  300. */
  301. public function setRules(array $rules)
  302. {
  303. foreach ($rules as &$rule) {
  304. if (substr($rule, -4) == 'Rule') {
  305. $rule = substr($rule, 0, -4);
  306. }
  307. }
  308. $this->rules = $rules;
  309. }
  310. /**
  311. * Add rule to enforce.
  312. *
  313. * @param string $rule
  314. * @return null
  315. */
  316. public function addRule($rule)
  317. {
  318. $this->rules[] = $rule;
  319. }
  320. /**
  321. * Returns the rules to enforce.
  322. *
  323. * @return array
  324. */
  325. public function getRules()
  326. {
  327. return $this->rules;
  328. }
  329. /**
  330. * Returns the list of files to skip.
  331. *
  332. * @return array
  333. */
  334. public function getSkipFiles()
  335. {
  336. return $this->skipFiles;
  337. }
  338. /**
  339. * Checks whether settings exist for given rule.
  340. *
  341. * @param string $rule
  342. * @return bool
  343. */
  344. public function hasSettings($rule)
  345. {
  346. return isset($this->ruleSettings[$rule]);
  347. }
  348. /**
  349. * Returns all settings for given rule.
  350. *
  351. * @param string $rule
  352. * @return array
  353. */
  354. public function getSettings($rule)
  355. {
  356. if (!isset($this->ruleSettings[$rule])) {
  357. return array();
  358. }
  359. $settings = $this->ruleSettings[$rule];
  360. if (isset($settings['skip'])) {
  361. $settings['skip'] = explode(',', $settings['skip']);
  362. $settings['skip'] = array_map('trim', $settings['skip']);
  363. foreach ($settings['skip'] as &$path) {
  364. $path = $this->toAbsolutePath($path);
  365. }
  366. }
  367. return $settings;
  368. }
  369. /**
  370. * Checks whether a certain setting exists for given rule.
  371. *
  372. * @param string $rule
  373. * @param string $setting
  374. * @return bool
  375. */
  376. public function hasSetting($rule, $setting)
  377. {
  378. return isset($this->ruleSettings[$rule]) && isset($this->ruleSettings[$rule][$setting]);
  379. }
  380. /**
  381. * Returns given setting for a given rule.
  382. *
  383. * @param string $rule
  384. * @param string $setting
  385. * @return string
  386. */
  387. public function getSetting($rule, $setting)
  388. {
  389. if (!isset($this->ruleSettings[$rule]) && !isset($this->ruleSettings[$rule][$setting])) {
  390. return '';
  391. }
  392. return $this->ruleSettings[$rule][$setting];
  393. }
  394. }
  395. ?>