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

Add a rule to check that arrays are hinted to doctrine #541

Open
wants to merge 2 commits into
base: 1.4.x
Choose a base branch
from

Conversation

BackEndTea
Copy link

I created this rule for our own code base because we forgot to properly hint to arrays when refactoring code to use doctrine. I figured it could be useful to others, and this seemed like the most logical repository for it.

If this is something you want in here i can expand the tests and improve support for the Statement class for example.

@BackEndTea BackEndTea force-pushed the feat/array-parameter-type-rule branch from 4b7aa3a to 462d7c1 Compare February 9, 2024 14:14
@BackEndTea BackEndTea force-pushed the feat/array-parameter-type-rule branch from ad4318e to e5cab77 Compare February 9, 2024 15:00
@ondrejmirtes
Copy link
Member

Can you please tell me more? What kind of bug happens if you don't hint the parameter type correctly?

@VincentLanglet
Copy link
Contributor

Can you please tell me more? What kind of bug happens if you don't hint the parameter type correctly?

When looking at the executeStatement method there is a line

if ($this->needsArrayParameterConversion($params, $types)) {
    [$sql, $params, $types] = $this->expandArrayParameters($sql, $params, $types);
}

with

private function needsArrayParameterConversion(array $params, array $types): bool
    {
        if (is_string(key($params))) {
            return true;
        }

        foreach ($types as $type) {
            if (
                $type === ArrayParameterType::INTEGER
                || $type === ArrayParameterType::STRING
                || $type === ArrayParameterType::ASCII
                || $type === ArrayParameterType::BINARY
            ) {
                return true;
            }
        }

        return false;
    }

So I assume, if you don't typehint the parameter as array, the value is not expanded which cause a wrong query.

@ondrejmirtes
Copy link
Member

Sorry, I didn't want details from Doctrine core (I don't understand it). I wanted to see some userland code and the implications of hinting/not hinting the types.

@BackEndTea
Copy link
Author

For us, if we don't add those hints, we get a warning of Array to string conversion since it tries to turn the array of parameters into a string.

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

Successfully merging this pull request may close these issues.

3 participants