Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assumption about value type causes parsing error #185

Open
rkeet opened this issue Mar 21, 2018 · 1 comment
Open

Assumption about value type causes parsing error #185

rkeet opened this issue Mar 21, 2018 · 1 comment

Comments

@rkeet
Copy link

rkeet commented Mar 21, 2018

} elseif (gettype($values[$property]) !== $type['type'] && !$values[$property] instanceof $type['type']) {

Line #810 is the elseif (...) line

        if ($type['type'] === 'array') {
            // handle the case of a single value
            if ( ! is_array($values[$property])) {
                $values[$property] = array($values[$property]);
            }
            // checks if the attribute has array type declaration, such as "array<string>"
            if (isset($type['array_type'])) {
                foreach ($values[$property] as $item) {
                    if (gettype($item) !== $type['array_type'] && !$item instanceof $type['array_type']) {
                        throw AnnotationException::attributeTypeError($property, $originalName, $this->context, 'either a(n) '.$type['array_type'].', or an array of '.$type['array_type'].'s', $item);
                    }
                }
            }
        } elseif (gettype($values[$property]) !== $type['type'] && !$values[$property] instanceof $type['type']) {
            throw AnnotationException::attributeTypeError($property, $originalName, $this->context, 'a(n) '.$type['value'], $values[$property]);
        }

Assumes type annotation of value to be done like so:

@var string

However, you could do something like

@var null|string

Because the elseif() only does string comparisons, using the latter notation will always fail.


Small use case

/**
 * @var string
 * @ORM\Column(name="api_pass", type="text", nullable=false)
 * @Encrypted(spices="credential")
 */
protected $apiPass;

Here the @Encrypted is custom Annotation. I'll leave all options out of the scope but it's defined like so:

/**
 * @Annotation
 * @Target("PROPERTY")
 */
class Encrypted
{
    /**
     * @var string
     */
    public $spices;
    
    // getters/setters + other options
}

However, I would like to define the property like so:

/**
 * @var null|string
 */
public $spices;

The first passes, the second fails because an assumption is done that the @var declaration always contains just one type.


P.s. - I've seen comments before about creating a test case, PR and PhpUnit tested code and such. If you have any docs on "how-to" all that, I'll give creating that a shot.

@jwage
Copy link
Member

jwage commented Mar 22, 2018

Hi again @rkeet :) Thanks for participating and creating issues for us to look at. You can find information on contributing here http://www.doctrine-project.org/contribute.html

@Majkl578 Majkl578 added this to the v2.0.0 milestone May 13, 2018
@alcaeus alcaeus removed this from the 2.0.0 milestone May 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants