File indexing completed on 2024-05-12 06:02:55

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Reflection
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Reflection_Class
0024  */
0025 // require_once 'Zend/Reflection/Class.php';
0026 
0027 /**
0028  * @see Zend_Reflection_Docblock
0029  */
0030 // require_once 'Zend/Reflection/Docblock.php';
0031 
0032 /**
0033  * @see Zend_Reflection_Parameter
0034  */
0035 // require_once 'Zend/Reflection/Parameter.php';
0036 
0037 /**
0038  * @category   Zend
0039  * @package    Zend_Reflection
0040  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0041  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0042  */
0043 class Zend_Reflection_Method extends ReflectionMethod
0044 {
0045     /**
0046      * Retrieve method docblock reflection
0047      *
0048      * @return Zend_Reflection_Docblock
0049      * @throws Zend_Reflection_Exception
0050      */
0051     public function getDocblock($reflectionClass = 'Zend_Reflection_Docblock')
0052     {
0053         if ('' == $this->getDocComment()) {
0054             // require_once 'Zend/Reflection/Exception.php';
0055             throw new Zend_Reflection_Exception($this->getName() . ' does not have a docblock');
0056         }
0057 
0058         $instance = new $reflectionClass($this);
0059         if (!$instance instanceof Zend_Reflection_Docblock) {
0060             // require_once 'Zend/Reflection/Exception.php';
0061             throw new Zend_Reflection_Exception('Invalid reflection class provided; must extend Zend_Reflection_Docblock');
0062         }
0063         return $instance;
0064     }
0065 
0066     /**
0067      * Get start line (position) of method
0068      *
0069      * @param  bool $includeDocComment
0070      * @return int
0071      */
0072     public function getStartLine($includeDocComment = false)
0073     {
0074         if ($includeDocComment) {
0075             if ($this->getDocComment() != '') {
0076                 return $this->getDocblock()->getStartLine();
0077             }
0078         }
0079 
0080         return parent::getStartLine();
0081     }
0082 
0083     /**
0084      * Get reflection of declaring class
0085      *
0086      * @param  string $reflectionClass Name of reflection class to use
0087      * @return Zend_Reflection_Class
0088      */
0089     public function getDeclaringClass($reflectionClass = 'Zend_Reflection_Class')
0090     {
0091         $phpReflection  = parent::getDeclaringClass();
0092         $zendReflection = new $reflectionClass($phpReflection->getName());
0093         if (!$zendReflection instanceof Zend_Reflection_Class) {
0094             // require_once 'Zend/Reflection/Exception.php';
0095             throw new Zend_Reflection_Exception('Invalid reflection class provided; must extend Zend_Reflection_Class');
0096         }
0097         unset($phpReflection);
0098         return $zendReflection;
0099     }
0100 
0101     /**
0102      * Get all method parameter reflection objects
0103      *
0104      * @param  string $reflectionClass Name of reflection class to use
0105      * @return array of Zend_Reflection_Parameter objects
0106      */
0107     public function getParameters($reflectionClass = 'Zend_Reflection_Parameter')
0108     {
0109         $phpReflections  = parent::getParameters();
0110         $zendReflections = array();
0111         while ($phpReflections && ($phpReflection = array_shift($phpReflections))) {
0112             $instance = new $reflectionClass(array($this->getDeclaringClass()->getName(), $this->getName()), $phpReflection->getName());
0113             if (!$instance instanceof Zend_Reflection_Parameter) {
0114                 // require_once 'Zend/Reflection/Exception.php';
0115                 throw new Zend_Reflection_Exception('Invalid reflection class provided; must extend Zend_Reflection_Parameter');
0116             }
0117             $zendReflections[] = $instance;
0118             unset($phpReflection);
0119         }
0120         unset($phpReflections);
0121         return $zendReflections;
0122     }
0123 
0124     /**
0125      * Get method contents
0126      *
0127      * @param  bool $includeDocblock
0128      * @return string
0129      */
0130     public function getContents($includeDocblock = true)
0131     {
0132         $fileContents = file($this->getFileName());
0133         $startNum = $this->getStartLine($includeDocblock);
0134         $endNum = ($this->getEndLine() - $this->getStartLine());
0135 
0136         return implode("\n", array_splice($fileContents, $startNum, $endNum, true));
0137     }
0138 
0139     /**
0140      * Get method body
0141      *
0142      * @return string
0143      */
0144     public function getBody()
0145     {
0146         $lines = array_slice(
0147             file($this->getDeclaringClass()->getFileName(), FILE_IGNORE_NEW_LINES),
0148             $this->getStartLine()-1,
0149             ($this->getEndLine() - $this->getStartLine()) + 1,
0150             true
0151         );
0152 
0153         // Strip off lines until we come to a closing bracket
0154         do {
0155             if (count($lines) == 0) break;
0156             $firstLine = array_shift($lines);
0157         } while (strpos($firstLine, ')') === false);
0158 
0159         // If the opening brace isn't on the same line as method 
0160         // signature, then we should pop off more lines until we find it
0161         if (strpos($firstLine,'{') === false) {
0162             do {
0163                 if (count($lines) == 0) break;
0164                 $firstLine = array_shift($lines);
0165             } while (strpos($firstLine, '{') === false);
0166         }
0167 
0168         // If there are more characters on the line after the opening brace,
0169         // push them back onto the lines stack as they are part of the body
0170         $restOfFirstLine = trim(substr($firstLine, strpos($firstLine, '{')+1));
0171         if (!empty($restOfFirstLine)) {
0172             array_unshift($lines, $restOfFirstLine);
0173         }
0174 
0175         $lastLine = array_pop($lines);
0176 
0177         // If there are more characters on the line before the closing brace,
0178         // push them back onto the lines stack as they are part of the body
0179         $restOfLastLine = trim(substr($lastLine, 0, strrpos($lastLine, '}')-1));
0180         if (!empty($restOfLastLine)) {
0181             array_push($lines, $restOfLastLine);
0182         }
0183 
0184         // just in case we had code on the bracket lines
0185         return implode("\n", $lines);
0186     }
0187 }