Jump to content

Reflectionproperty Linenumber/position


hProj

Recommended Posts

Hello everybody,

 

I'm writing a Symfony2 command to generate classes and retain code from existent files that it tagged with a certain annotation. I'm using Reflection to inspect the existent classes.

So far, I've managed to retain methods, but properties are giving me issues.

 

This is my code:

 

protected function retainCode($from, $to)
{
 $class = new \ReflectionClass($from);
 $file = $class->getFileName();
 $stream=fopen($to, 'w');
//Check properties
 foreach ($class->getProperties() as $property)
 {
	 $reader = new \Doctrine\Common\Annotations\AnnotationReader();
	 $annotation = $reader->getPropertyAnnotations($property, 'RetainCode');
	 if ($annotation != null)
	 {
		 $contents = file($file);
		 $start_line = $property->getStartLine() - 1;
		 $prefix=$property->getDocComment();
		 $code = PHP_EOL.' '.$prefix.PHP_EOL.implode("", array_slice($contents, $start_line, ($property->getEndLine()-$start_line)));
		 fwrite($stream, $code);
	 }
 }
//Check methods
 foreach ($class->getMethods() as $method)
 {
	 $reader = new \Doctrine\Common\Annotations\AnnotationReader();
	 $annotation = $reader->getMethodAnnotations($method, 'RetainCode');
	 if ($annotation != null)
	 {
		 $contents = file($file);
		 $start_line = $method->getStartLine() - 1;
		 $prefix=$method->getDocComment();
		 $code = PHP_EOL.' '.$prefix.PHP_EOL.implode("", array_slice($contents, $start_line, ($method->getEndLine()-$start_line)));
		 fwrite($stream, $code);
	 }
 }
 fclose($stream);
}

 

The implementation for methods works, but \ReflectionProperty does not contain any methods to get the position of the defined property. (Seems to me it should actually have such option)

Is there any way to get this, in a proper way?

 

Thanks in advance!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.