Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Code / Scanner / MethodScanner.php
blob6e39320dcd5e1d070f9a125fca089e2a6f5791f0
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Code\Scanner;
12 use Zend\Code\Annotation\AnnotationManager;
13 use Zend\Code\Exception;
14 use Zend\Code\NameInformation;
16 class MethodScanner implements ScannerInterface
18 /**
19 * @var bool
21 protected $isScanned = false;
23 /**
24 * @var string
26 protected $docComment = null;
28 /**
29 * @var ClassScanner
31 protected $scannerClass = null;
33 /**
34 * @var string
36 protected $class = null;
38 /**
39 * @var string
41 protected $name = null;
43 /**
44 * @var int
46 protected $lineStart = null;
48 /**
49 * @var int
51 protected $lineEnd = null;
53 /**
54 * @var bool
56 protected $isFinal = false;
58 /**
59 * @var bool
61 protected $isAbstract = false;
63 /**
64 * @var bool
66 protected $isPublic = true;
68 /**
69 * @var bool
71 protected $isProtected = false;
73 /**
74 * @var bool
76 protected $isPrivate = false;
78 /**
79 * @var bool
81 protected $isStatic = false;
83 /**
84 * @var string
86 protected $body = '';
88 /**
89 * @var array
91 protected $tokens = array();
93 /**
94 * @var NameInformation
96 protected $nameInformation = null;
98 /**
99 * @var array
101 protected $infos = array();
104 * @param array $methodTokens
105 * @param NameInformation $nameInformation
107 public function __construct(array $methodTokens, NameInformation $nameInformation = null)
109 $this->tokens = $methodTokens;
110 $this->nameInformation = $nameInformation;
114 * @param string $class
115 * @return MethodScanner
117 public function setClass($class)
119 $this->class = (string) $class;
120 return $this;
124 * @param ClassScanner $scannerClass
125 * @return MethodScanner
127 public function setScannerClass(ClassScanner $scannerClass)
129 $this->scannerClass = $scannerClass;
130 return $this;
134 * @return MethodScanner
136 public function getClassScanner()
138 return $this->scannerClass;
142 * @return string
144 public function getName()
146 $this->scan();
148 return $this->name;
152 * @return int
154 public function getLineStart()
156 $this->scan();
158 return $this->lineStart;
162 * @return int
164 public function getLineEnd()
166 $this->scan();
168 return $this->lineEnd;
172 * @return string
174 public function getDocComment()
176 $this->scan();
178 return $this->docComment;
182 * @param AnnotationManager $annotationManager
183 * @return AnnotationScanner
185 public function getAnnotations(AnnotationManager $annotationManager)
187 if (($docComment = $this->getDocComment()) == '') {
188 return false;
191 return new AnnotationScanner($annotationManager, $docComment, $this->nameInformation);
195 * @return bool
197 public function isFinal()
199 $this->scan();
201 return $this->isFinal;
205 * @return bool
207 public function isAbstract()
209 $this->scan();
211 return $this->isAbstract;
215 * @return bool
217 public function isPublic()
219 $this->scan();
221 return $this->isPublic;
225 * @return bool
227 public function isProtected()
229 $this->scan();
231 return $this->isProtected;
235 * @return bool
237 public function isPrivate()
239 $this->scan();
241 return $this->isPrivate;
245 * @return bool
247 public function isStatic()
249 $this->scan();
251 return $this->isStatic;
255 * @return int
257 public function getNumberOfParameters()
259 return count($this->getParameters());
263 * @param bool $returnScanner
264 * @return array
266 public function getParameters($returnScanner = false)
268 $this->scan();
270 $return = array();
272 foreach ($this->infos as $info) {
273 if ($info['type'] != 'parameter') {
274 continue;
277 if (!$returnScanner) {
278 $return[] = $info['name'];
279 } else {
280 $return[] = $this->getParameter($info['name']);
284 return $return;
288 * @param int|string $parameterNameOrInfoIndex
289 * @return ParameterScanner
290 * @throws Exception\InvalidArgumentException
292 public function getParameter($parameterNameOrInfoIndex)
294 $this->scan();
296 if (is_int($parameterNameOrInfoIndex)) {
297 $info = $this->infos[$parameterNameOrInfoIndex];
298 if ($info['type'] != 'parameter') {
299 throw new Exception\InvalidArgumentException('Index of info offset is not about a parameter');
301 } elseif (is_string($parameterNameOrInfoIndex)) {
302 foreach ($this->infos as $info) {
303 if ($info['type'] === 'parameter' && $info['name'] === $parameterNameOrInfoIndex) {
304 break;
306 unset($info);
308 if (!isset($info)) {
309 throw new Exception\InvalidArgumentException('Index of info offset is not about a parameter');
313 $p = new ParameterScanner(
314 array_slice($this->tokens, $info['tokenStart'], $info['tokenEnd'] - $info['tokenStart']),
315 $this->nameInformation
317 $p->setDeclaringFunction($this->name);
318 $p->setDeclaringScannerFunction($this);
319 $p->setDeclaringClass($this->class);
320 $p->setDeclaringScannerClass($this->scannerClass);
321 $p->setPosition($info['position']);
323 return $p;
327 * @return string
329 public function getBody()
331 $this->scan();
333 return $this->body;
336 public static function export()
338 // @todo
341 public function __toString()
343 $this->scan();
345 return var_export($this, true);
348 protected function scan()
350 if ($this->isScanned) {
351 return;
354 if (!$this->tokens) {
355 throw new Exception\RuntimeException('No tokens were provided');
359 * Variables & Setup
362 $tokens = &$this->tokens; // localize
363 $infos = &$this->infos; // localize
364 $tokenIndex = null;
365 $token = null;
366 $tokenType = null;
367 $tokenContent = null;
368 $tokenLine = null;
369 $infoIndex = 0;
370 $parentCount = 0;
373 * MACRO creation
375 $MACRO_TOKEN_ADVANCE = function () use (&$tokens, &$tokenIndex, &$token, &$tokenType, &$tokenContent, &$tokenLine) {
376 static $lastTokenArray = null;
377 $tokenIndex = ($tokenIndex === null) ? 0 : $tokenIndex + 1;
378 if (!isset($tokens[$tokenIndex])) {
379 $token = false;
380 $tokenContent = false;
381 $tokenType = false;
382 $tokenLine = false;
384 return false;
386 $token = $tokens[$tokenIndex];
387 if (is_string($token)) {
388 $tokenType = null;
389 $tokenContent = $token;
390 $tokenLine = $tokenLine + substr_count($lastTokenArray[1],
391 "\n"); // adjust token line by last known newline count
392 } else {
393 list($tokenType, $tokenContent, $tokenLine) = $token;
396 return $tokenIndex;
398 $MACRO_INFO_START = function () use (&$infoIndex, &$infos, &$tokenIndex, &$tokenLine) {
399 $infos[$infoIndex] = array(
400 'type' => 'parameter',
401 'tokenStart' => $tokenIndex,
402 'tokenEnd' => null,
403 'lineStart' => $tokenLine,
404 'lineEnd' => $tokenLine,
405 'name' => null,
406 'position' => $infoIndex + 1, // position is +1 of infoIndex
409 $MACRO_INFO_ADVANCE = function () use (&$infoIndex, &$infos, &$tokenIndex, &$tokenLine) {
410 $infos[$infoIndex]['tokenEnd'] = $tokenIndex;
411 $infos[$infoIndex]['lineEnd'] = $tokenLine;
412 $infoIndex++;
414 return $infoIndex;
418 * START FINITE STATE MACHINE FOR SCANNING TOKENS
421 // Initialize token
422 $MACRO_TOKEN_ADVANCE();
424 SCANNER_TOP:
426 $this->lineStart = ($this->lineStart) ? : $tokenLine;
428 switch ($tokenType) {
429 case T_DOC_COMMENT:
430 $this->lineStart = null;
431 if ($this->docComment === null && $this->name === null) {
432 $this->docComment = $tokenContent;
434 goto SCANNER_CONTINUE_SIGNATURE;
436 case T_FINAL:
437 $this->isFinal = true;
438 goto SCANNER_CONTINUE_SIGNATURE;
440 case T_ABSTRACT:
441 $this->isAbstract = true;
442 goto SCANNER_CONTINUE_SIGNATURE;
444 case T_PUBLIC:
445 // use defaults
446 goto SCANNER_CONTINUE_SIGNATURE;
448 case T_PROTECTED:
449 $this->isProtected = true;
450 $this->isPublic = false;
451 goto SCANNER_CONTINUE_SIGNATURE;
453 case T_PRIVATE:
454 $this->isPrivate = true;
455 $this->isPublic = false;
456 goto SCANNER_CONTINUE_SIGNATURE;
458 case T_STATIC:
459 $this->isStatic = true;
460 goto SCANNER_CONTINUE_SIGNATURE;
462 case T_VARIABLE:
463 case T_STRING:
465 if ($tokenType === T_STRING && $parentCount === 0) {
466 $this->name = $tokenContent;
469 if ($parentCount === 1) {
470 if (!isset($infos[$infoIndex])) {
471 $MACRO_INFO_START();
473 if ($tokenType === T_VARIABLE) {
474 $infos[$infoIndex]['name'] = ltrim($tokenContent, '$');
478 goto SCANNER_CONTINUE_SIGNATURE;
480 case null:
482 switch ($tokenContent) {
483 case '&':
484 if (!isset($infos[$infoIndex])) {
485 $MACRO_INFO_START();
487 goto SCANNER_CONTINUE_SIGNATURE;
488 case '(':
489 $parentCount++;
490 goto SCANNER_CONTINUE_SIGNATURE;
491 case ')':
492 $parentCount--;
493 if ($parentCount === 0) {
494 if ($infos) {
495 $MACRO_INFO_ADVANCE();
497 $context = 'body';
499 goto SCANNER_CONTINUE_BODY;
500 case ',':
501 if ($parentCount === 1) {
502 $MACRO_INFO_ADVANCE();
504 goto SCANNER_CONTINUE_SIGNATURE;
508 SCANNER_CONTINUE_SIGNATURE:
510 if ($MACRO_TOKEN_ADVANCE() === false) {
511 goto SCANNER_END;
513 goto SCANNER_TOP;
515 SCANNER_CONTINUE_BODY:
517 $braceCount = 0;
518 while ($MACRO_TOKEN_ADVANCE() !== false) {
519 if ($tokenContent == '}') {
520 $braceCount--;
522 if ($braceCount > 0) {
523 $this->body .= $tokenContent;
525 if ($tokenContent == '{') {
526 $braceCount++;
528 $this->lineEnd = $tokenLine;
531 SCANNER_END:
533 $this->isScanned = true;
535 return;