PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/php/pear/PHP/Depend/Code/File.php

https://github.com/KaRLsM/SIFO
PHP | 423 lines | 128 code | 36 blank | 259 comment | 5 complexity | 994da4f25981918b0e92a9625eb3e9b1 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of PHP_Depend.
  4. *
  5. * PHP Version 5
  6. *
  7. * Copyright (c) 2008-2011, Manuel Pichler <mapi@pdepend.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 QualityAssurance
  40. * @package PHP_Depend
  41. * @subpackage Code
  42. * @author Manuel Pichler <mapi@pdepend.org>
  43. * @copyright 2008-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://pdepend.org/
  47. */
  48. /**
  49. * This class provides an interface to a single source file.
  50. *
  51. * @category QualityAssurance
  52. * @package PHP_Depend
  53. * @subpackage Code
  54. * @author Manuel Pichler <mapi@pdepend.org>
  55. * @copyright 2008-2011 Manuel Pichler. All rights reserved.
  56. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  57. * @version Release: 0.10.6
  58. * @link http://pdepend.org/
  59. */
  60. class PHP_Depend_Code_File implements PHP_Depend_Code_NodeI
  61. {
  62. /**
  63. * The type of this class.
  64. *
  65. * @since 0.10.0
  66. */
  67. const CLAZZ = __CLASS__;
  68. /**
  69. * The internal used cache instance.
  70. *
  71. * @var PHP_Depend_Util_Cache_Driver
  72. * @since 0.10.0
  73. */
  74. protected $cache = null;
  75. /**
  76. * The unique identifier for this function.
  77. *
  78. * @var string
  79. */
  80. protected $uuid = null;
  81. /**
  82. * The source file name/path.
  83. *
  84. * @var string
  85. */
  86. protected $fileName = null;
  87. /**
  88. * The comment for this type.
  89. *
  90. * @var string
  91. */
  92. protected $docComment = null;
  93. /**
  94. * The files start line. This property must always have the value <em>1</em>.
  95. *
  96. * @var integer
  97. * @since 0.10.0
  98. */
  99. protected $startLine = 0;
  100. /**
  101. * The files end line.
  102. *
  103. * @var integer
  104. * @since 0.10.0
  105. */
  106. protected $endLine = 0;
  107. /**
  108. * List of classes, interfaces and functions that parsed from this file.
  109. *
  110. * @var array(PHP_Depend_Code_AbstractItem)
  111. * @since 0.10.0
  112. */
  113. protected $childNodes = array();
  114. /**
  115. * Was this file instance restored from the cache?
  116. *
  117. * @var boolean
  118. * @since 0.10.0
  119. */
  120. protected $cached = false;
  121. /**
  122. * Normalized code in this file.
  123. *
  124. * @var string $_source
  125. */
  126. private $_source = null;
  127. /**
  128. * Constructs a new source file instance.
  129. *
  130. * @param string $fileName The source file name/path.
  131. */
  132. public function __construct($fileName)
  133. {
  134. if ($fileName !== null) {
  135. $this->fileName = realpath($fileName);
  136. }
  137. }
  138. /**
  139. * Returns the physical file name for this object.
  140. *
  141. * @return string
  142. */
  143. public function getName()
  144. {
  145. return $this->fileName;
  146. }
  147. /**
  148. * Returns the physical file name for this object.
  149. *
  150. * @return string
  151. */
  152. public function getFileName()
  153. {
  154. return $this->fileName;
  155. }
  156. /**
  157. * Returns a uuid for this code node.
  158. *
  159. * @return string
  160. */
  161. public function getUUID()
  162. {
  163. return $this->uuid;
  164. }
  165. /**
  166. * Sets the unique identifier for this file instance.
  167. *
  168. * @param string $uuid Identifier for this file.
  169. *
  170. * @return void
  171. * @since 0.9.12
  172. */
  173. public function setUUID($uuid)
  174. {
  175. $this->uuid = $uuid;
  176. }
  177. /**
  178. * Setter method for the used parser and token cache.
  179. *
  180. * @param PHP_Depend_Util_Cache_Driver $cache A cache driver instance.
  181. *
  182. * @return PHP_Depend_Code_File
  183. * @since 0.10.0
  184. */
  185. public function setCache(PHP_Depend_Util_Cache_Driver $cache)
  186. {
  187. $this->cache = $cache;
  188. return $this;
  189. }
  190. /**
  191. * Returns normalized source code with stripped whitespaces.
  192. *
  193. * @return array(integer=>string)
  194. */
  195. public function getSource()
  196. {
  197. $this->readSource();
  198. return $this->_source;
  199. }
  200. /**
  201. * Returns an <b>array</b> with all tokens within this file.
  202. *
  203. * @return array(array)
  204. */
  205. public function getTokens()
  206. {
  207. return (array) $this->cache
  208. ->type('tokens')
  209. ->restore($this->getUUID());
  210. }
  211. /**
  212. * Sets the tokens for this file.
  213. *
  214. * @param array(array) $tokens The generated tokens.
  215. *
  216. * @return void
  217. */
  218. public function setTokens(array $tokens)
  219. {
  220. return $this->cache
  221. ->type('tokens')
  222. ->store($this->getUUID(), $tokens);
  223. }
  224. /**
  225. * Returns the doc comment for this item or <b>null</b>.
  226. *
  227. * @return string
  228. */
  229. public function getDocComment()
  230. {
  231. return $this->docComment;
  232. }
  233. /**
  234. * Sets the doc comment for this item.
  235. *
  236. * @param string $docComment The doc comment block.
  237. *
  238. * @return void
  239. */
  240. public function setDocComment($docComment)
  241. {
  242. $this->docComment = $docComment;
  243. }
  244. /**
  245. * Adds a source item that was parsed from this source file.
  246. *
  247. * @param PHP_Depend_Code_AbstractItem $item Node parsed in this file.
  248. *
  249. * @return void
  250. * @since 0.10.0
  251. */
  252. public function addChild(PHP_Depend_Code_AbstractItem $item)
  253. {
  254. $this->childNodes[$item->getUUID()] = $item;
  255. }
  256. /**
  257. * Returns the start line number for this source file. For an existing file
  258. * this value must always be <em>1</em>, while it can be <em>0</em> for a
  259. * not existing dummy file.
  260. *
  261. * @return integer
  262. * @since 0.10.0
  263. */
  264. public function getStartLine()
  265. {
  266. if ($this->startLine === 0) {
  267. $this->readSource();
  268. }
  269. return $this->startLine;
  270. }
  271. /**
  272. * Returns the start line number for this source file. For an existing file
  273. * this value must always be greater <em>0</em>, while it can be <em>0</em>
  274. * for a not existing dummy file.
  275. *
  276. * @return integer
  277. * @since 0.10.0
  278. */
  279. public function getEndLine()
  280. {
  281. if ($this->endLine === 0) {
  282. $this->readSource();
  283. }
  284. return $this->endLine;
  285. }
  286. /**
  287. * This method will return <b>true</b> when this file instance was restored
  288. * from the cache and not currently parsed. Otherwise this method will return
  289. * <b>false</b>.
  290. *
  291. * @return boolean
  292. * @since 0.10.0
  293. */
  294. public function isCached()
  295. {
  296. return $this->cached;
  297. }
  298. /**
  299. * Visitor method for node tree traversal.
  300. *
  301. * @param PHP_Depend_VisitorI $visitor The context visitor
  302. * implementation.
  303. *
  304. * @return void
  305. */
  306. public function accept(PHP_Depend_VisitorI $visitor)
  307. {
  308. $visitor->visitFile($this);
  309. }
  310. /**
  311. * The magic sleep method will be called by PHP's runtime environment right
  312. * before it serializes an instance of this class. This method returns an
  313. * array with those property names that should be serialized.
  314. *
  315. * @return array(string)
  316. * @since 0.10.0
  317. */
  318. public function __sleep()
  319. {
  320. return array(
  321. 'cache',
  322. 'childNodes',
  323. 'docComment',
  324. 'endLine',
  325. 'fileName',
  326. 'startLine',
  327. 'uuid'
  328. );
  329. }
  330. /**
  331. * The magic wakeup method will is called by PHP's runtime environment when
  332. * a serialized instance of this class was unserialized. This implementation
  333. * of the wakeup method restores the references between all parsed entities
  334. * in this source file and this file instance.
  335. *
  336. * @return void
  337. * @since 0.10.0
  338. * @see PHP_Depend_Code_File::$childNodes
  339. */
  340. public function __wakeup()
  341. {
  342. $this->cached = true;
  343. foreach ($this->childNodes as $childNode) {
  344. $childNode->setSourceFile($this);
  345. }
  346. }
  347. /**
  348. * Returns the string representation of this class.
  349. *
  350. * @return string
  351. */
  352. public function __toString()
  353. {
  354. return ($this->fileName === null ? '' : $this->fileName);
  355. }
  356. /**
  357. * Reads the source file if required.
  358. *
  359. * @return void
  360. */
  361. protected function readSource()
  362. {
  363. if ($this->_source === null && file_exists($this->fileName)) {
  364. $source = file_get_contents($this->fileName);
  365. $this->_source = str_replace(array("\r\n", "\r"), "\n", $source);
  366. $this->startLine = 1;
  367. $this->endLine = substr_count($this->_source, "\n") + 1;
  368. }
  369. }
  370. // Deprecated methods
  371. // @codeCoverageIgnoreStart
  372. /**
  373. * This method can be called by the PHP_Depend runtime environment or a
  374. * utilizing component to free up memory. This methods are required for
  375. * PHP version < 5.3 where cyclic references can not be resolved
  376. * automatically by PHP's garbage collector.
  377. *
  378. * @return void
  379. * @since 0.9.12
  380. * @deprecated Since 0.10.0
  381. */
  382. public function free()
  383. {
  384. fwrite(STDERR, __METHOD__ . ' is deprecated since version 0.10.0' . PHP_EOL);
  385. }
  386. // @codeCoverageIgnoreEnd
  387. }