<commit_before><?php

namespace Papper\Internal;

use TokenReflection\Broker;
use TokenReflection\Broker\Backend\Memory;

class AnnotationTypeReader
{
	const TYPE_REGEXP = '/^[\\\\\w\d]+/i';
	const PROPERTY_ANNOTATION_NAME = 'var';
	const METHOD_ANNOTATION_NAME = 'method';

	private $broker;

	public function __construct()
	{
		$this->broker = new Broker(new Memory());
	}

	/**
	 * @param \ReflectionProperty|\ReflectionMethod $reflector
	 * @throws \Exception
	 * @throws \TokenReflection\Exception\BrokerException
	 * @throws \TokenReflection\Exception\ParseException
	 * @throws \TokenReflection\Exception\RuntimeException
	 * @return string
	 */
	public function getType($reflector)
	{
		$declaringClass = $reflector->getDeclaringClass();

		$this->broker->processFile($declaringClass->getFileName());
		$class = $this->broker->getClass($declaringClass->name);

		if ($reflector instanceof \ReflectionProperty) {
			$name = 'var';
			$annotations = $class->getProperty($reflector->name)->getAnnotations();
		} elseif ($reflector instanceof \ReflectionMethod) {
			$name = 'return';
			$annotations = $class->getMethod($reflector->name)->getAnnotations();
		} else {
			return null;
		}

		if (!isset($annotations[$name])) {
			return null;
		}

		return $this->parseType($annotations[$name], $class->getNamespaceName(), $class->getNamespaceAliases());
	}

	private function parseType($annotations, $namespace, array $namespaceAliases)
	{
		foreach ($annotations as $annotation) {
			preg_match(self::TYPE_REGEXP, $annotation, $matches);
			if (!empty($matches[0])) {
				list($path, $class) = $this->parseClassPath($matches[0]);

				if (empty($path) && isset($namespaceAliases[$class])) {
					return $namespaceAliases[$class];
				} else if (isset($namespaceAliases[$path])) {
					return $namespaceAliases[$path] . '\\' . $class;
				} else if (class_exists($namespace . '\\' . $class)) {
					return $namespace . '\\' . $class;
				} else if (class_exists($class)) {
					return $class;
				}
			}
		}
		return null;
	}

	private function parseClassPath($classPath)
	{
		$pos = strpos($classPath, "\\");
		return ($pos !== false)
			? array(substr($classPath, 0, $pos), substr($classPath, $pos+1))
			: array('', $classPath);
	}
}
<commit_msg>Add ignoring of resolving scalar type in annotation hints<commit_after><?php

namespace Papper\Internal;

use TokenReflection\Broker;
use TokenReflection\Broker\Backend\Memory;

class AnnotationTypeReader
{
	const TYPE_REGEXP = '/^[\\\\\w\d]+/i';
	const PROPERTY_ANNOTATION_NAME = 'var';
	const METHOD_ANNOTATION_NAME = 'return';

	private static $ignoredTypes = array(
		'boolean',
		'bool',
		'integer',
		'int',
		'float',
		'double',
		'string',
		'array',
		'object',
		'null'
	);

	private $broker;

	public function __construct()
	{
		$this->broker = new Broker(new Memory());
	}

	/**
	 * @param \ReflectionProperty|\ReflectionMethod $reflector
	 * @throws \Exception
	 * @throws \TokenReflection\Exception\BrokerException
	 * @throws \TokenReflection\Exception\ParseException
	 * @throws \TokenReflection\Exception\RuntimeException
	 * @return string
	 */
	public function getType($reflector)
	{
		$declaringClass = $reflector->getDeclaringClass();

		$this->broker->processFile($declaringClass->getFileName());
		$class = $this->broker->getClass($declaringClass->name);

		if ($reflector instanceof \ReflectionProperty) {
			$name = self::PROPERTY_ANNOTATION_NAME;
			$annotations = $class->getProperty($reflector->name)->getAnnotations();
		} elseif ($reflector instanceof \ReflectionMethod) {
			$name = self::METHOD_ANNOTATION_NAME;
			$annotations = $class->getMethod($reflector->name)->getAnnotations();
		} else {
			return null;
		}

		if (!isset($annotations[$name])) {
			return null;
		}

		return $this->parseType($annotations[$name], $class->getNamespaceName(), $class->getNamespaceAliases());
	}

	private function parseType($annotations, $namespace, array $namespaceAliases)
	{
		foreach ($annotations as $annotation) {
			preg_match(self::TYPE_REGEXP, $annotation, $matches);
			if (!empty($matches[0])) {
				list($path, $class) = $this->parseClassPath($matches[0]);

				if ($this->isIgnoredType($class)) {
					continue;
				}

				if (empty($path) && isset($namespaceAliases[$class])) {
					return $namespaceAliases[$class];
				} else if (isset($namespaceAliases[$path])) {
					return $namespaceAliases[$path] . '\\' . $class;
				} else if (class_exists($namespace . '\\' . $class)) {
					return $namespace . '\\' . $class;
				} else if (class_exists($class)) {
					return $class;
				}
			}
		}
		return null;
	}

	private function parseClassPath($classPath)
	{
		$pos = strpos($classPath, "\\");
		return ($pos !== false)
			? array(substr($classPath, 0, $pos), substr($classPath, $pos+1))
			: array('', $classPath);
	}

	private function isIgnoredType($type)
	{
		return in_array($type, self::$ignoredTypes);
	}
}

{'fraction_non_alphanumeric': 0.16788916055419723, 'fraction_numerical': 0.0016299918500407497, 'mean_word_length': 4.4303097345132745, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5441387', 'n_tokens_mistral': 1745, 'n_tokens_neox': 1540, 'n_words': 297}