Kdyby\Doctrine\Helpers::getPropertyLine PHP Method

getPropertyLine() public static method

public static getPropertyLine ( ReflectionProperty $property ) : integer
$property ReflectionProperty
return integer
    public static function getPropertyLine(\ReflectionProperty $property)
    {
        $class = $property->getDeclaringClass();
        $context = 'file';
        $contextBrackets = 0;
        foreach (token_get_all(file_get_contents($class->getFileName())) as $token) {
            if ($token === '{') {
                $contextBrackets += 1;
            } elseif ($token === '}') {
                $contextBrackets -= 1;
            }
            if (!is_array($token)) {
                continue;
            }
            if ($token[0] === T_CLASS) {
                $context = 'class';
                $contextBrackets = 0;
            } elseif ($context === 'class' && $contextBrackets === 1 && $token[0] === T_VARIABLE) {
                if ($token[1] === '$' . $property->getName()) {
                    return $token[2];
                }
            }
        }
        return NULL;
    }

Usage Example

Esempio n. 1
1
 /**
  * @param \Doctrine\Common\Annotations\AnnotationException $e
  *
  * @return string
  */
 public static function highlightAnnotationLine(AnnotationException $e)
 {
     foreach ($e->getTrace() as $step) {
         if (@$step['class'] . @$step['type'] . @$step['function'] !== 'Doctrine\\Common\\Annotations\\DocParser->parse') {
             continue;
         }
         $context = Strings::match($step['args'][1], '~^(?P<type>[^\\s]+)\\s*(?P<class>[^:]+)(?:::\\$?(?P<property>[^\\(]+))?$~i');
         break;
     }
     if (!isset($context)) {
         return FALSE;
     }
     $refl = Nette\Reflection\ClassType::from($context['class']);
     $file = $refl->getFileName();
     $line = NULL;
     if ($context['type'] === 'property') {
         $refl = $refl->getProperty($context['property']);
         $line = Kdyby\Doctrine\Helpers::getPropertyLine($refl);
     } elseif ($context['type'] === 'method') {
         $refl = $refl->getProperty($context['method']);
     }
     if (($errorLine = self::calculateErrorLine($refl, $e, $line)) === NULL) {
         return FALSE;
     }
     $dump = BlueScreen::highlightFile($file, $errorLine);
     return '<p><b>File:</b> ' . self::editorLink($file, $errorLine) . '</p>' . $dump;
 }