<commit_before><?php declare(strict_types = 1);

namespace DoctrineAnnotationCodingStandard\Sniffs\Commenting;

use DoctrineAnnotationCodingStandard\Helper\DocBlockHelper;
use DoctrineAnnotationCodingStandard\Helper\DoctrineMappingHelper;
use DoctrineAnnotationCodingStandard\Helper\TypeHelper;
use DoctrineAnnotationCodingStandard\Types\QualifyableObjectType;
use DoctrineAnnotationCodingStandard\Types\Type;
use PHP_CodeSniffer\Files\File;

class VarTagSniff extends AbstractDoctrineAnnotationSniff
{
    const CODE_NO_VAR_TAG = 'NoVarTag';
    const CODE_WRONG_VAR_TAG = 'WrongVarTag';

    /**
     * @param File $phpcsFile
     * @param int $stackPtr
     * @param array $annotations
     */
    protected function sniffDocblock(File $phpcsFile, int $stackPtr, array $annotations)
    {
        if (!DoctrineMappingHelper::isDoctrineMappedProperty($annotations)) {
            return;
        }

        $varTagContent = DocBlockHelper::getVarTagContent($phpcsFile, $stackPtr);

        if ($varTagContent === null) {
            $error = 'There must be a @var tag on Doctrine mapped properties';
            $phpcsFile->addError($error, $stackPtr, self::CODE_NO_VAR_TAG);

            return;
        }

        $expectedType = $this->qualify(DoctrineMappingHelper::getMappedType($annotations));
        $actualType = TypeHelper::fromString($varTagContent, null, $this->getImports());

        if (!$expectedType->isEqual($actualType)) {
            $error = \sprintf(
                'Expected @var type of "%s", got "%s"',
                $expectedType->toString($this->getNamespace(), $this->getImports()),
                $actualType->toString($this->getNamespace(), $this->getImports())
            );
            $phpcsFile->addError($error, $stackPtr, self::CODE_WRONG_VAR_TAG);
        }
    }

    /**
     * @param Type $type
     * @return Type
     */
    private function qualify(Type $type): Type
    {
        if (!$type instanceof QualifyableObjectType) {
            return $type;
        }

        return $type->qualify($this->getNamespace(), $this->getImports());
    }
}
<commit_msg>fix namespace based qualification<commit_after><?php declare(strict_types = 1);

namespace DoctrineAnnotationCodingStandard\Sniffs\Commenting;

use DoctrineAnnotationCodingStandard\Helper\DocBlockHelper;
use DoctrineAnnotationCodingStandard\Helper\DoctrineMappingHelper;
use DoctrineAnnotationCodingStandard\Helper\TypeHelper;
use DoctrineAnnotationCodingStandard\Types\QualifyableObjectType;
use DoctrineAnnotationCodingStandard\Types\Type;
use PHP_CodeSniffer\Files\File;

class VarTagSniff extends AbstractDoctrineAnnotationSniff
{
    const CODE_NO_VAR_TAG = 'NoVarTag';
    const CODE_WRONG_VAR_TAG = 'WrongVarTag';

    /**
     * @param File $phpcsFile
     * @param int $stackPtr
     * @param array $annotations
     */
    protected function sniffDocblock(File $phpcsFile, int $stackPtr, array $annotations)
    {
        if (!DoctrineMappingHelper::isDoctrineMappedProperty($annotations)) {
            return;
        }

        $varTagContent = DocBlockHelper::getVarTagContent($phpcsFile, $stackPtr);

        if ($varTagContent === null) {
            $error = 'There must be a @var tag on Doctrine mapped properties';
            $phpcsFile->addError($error, $stackPtr, self::CODE_NO_VAR_TAG);

            return;
        }

        $expectedType = $this->qualify(DoctrineMappingHelper::getMappedType($annotations));
        $actualType = TypeHelper::fromString($varTagContent, $this->getNamespace(), $this->getImports());

        if (!$expectedType->isEqual($actualType)) {
            $error = \sprintf(
                'Expected @var type of "%s", got "%s"',
                $expectedType->toString($this->getNamespace(), $this->getImports()),
                $actualType->toString($this->getNamespace(), $this->getImports())
            );
            $phpcsFile->addError($error, $stackPtr, self::CODE_WRONG_VAR_TAG);
        }
    }

    /**
     * @param Type $type
     * @return Type
     */
    private function qualify(Type $type): Type
    {
        if (!$type instanceof QualifyableObjectType) {
            return $type;
        }

        return $type->qualify($this->getNamespace(), $this->getImports());
    }
}

{'fraction_non_alphanumeric': 0.12003758515386423, 'fraction_numerical': 0.0004698144233027954, 'mean_word_length': 3.211671612265084, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 32, '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': '5218607', 'n_tokens_mistral': 1215, 'n_tokens_neox': 1193, 'n_words': 225}