Skip to content

Missing typehints reports when suppressed #51

@simPod

Description

@simPod

So far we have had disabled missing typehints rule:

- "~^(Method|Property) .+::.+(\\(\\))? (has parameter \\$\\w+ with no|has no return|has no) typehint specified$~"

Because it reported errors also for classes where we disabled the same reporting with @phpssSuppress used for CodeSniffer. And that was disabled for a reason.

For example, we have overriden PHPUnit method

    /**
     * Returns a test double for the specified class.
     *
     * @param string|string[] $originalClassName
     *
     * @throws Exception
     * @throws \InvalidArgumentException
     */
    protected function createMock($originalClassName): MockObject
    {
        return $this->getMockBuilder($originalClassName)
                    ->disableOriginalConstructor()
                    ->disableOriginalClone()
                    ->disableArgumentCloning()
                    ->disallowMockingUnknownTypes()
                    ->getMock();
    }

with

    /**
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
     */
    protected function createMock($originalClassName) : MockObject
    {
        throw new \BadMethodCallException('Use Mockery instead.');
    }

We suppresed type hint checks because we have to follow parent signature from external library so types cannot be added.

However, this PHPStan strict check is very handy but also it's very exhausting to manually enumarete all places to ignoreErrors in config.

As example I have modified the MissingMethodParameterTypehintRule rule in a following way

    private function checkMethodParameter(MethodReflection $methodReflection, ParameterReflection $parameterReflection, ?Doc $doc): ?string
    {
        $parameterType = $parameterReflection->getType();

        if (
            $parameterType instanceof MixedType &&
            !$parameterType->isExplicitMixed() &&
            (
                $doc === null ||
                preg_match(
                    '~(@phpcsSuppress SlevomatCodingStandard\.TypeHints\.TypeHintDeclaration\.MissingParameterTypeHint|@inheritdoc)~',
                    $doc->getText()
                ) === 0
            )
        ) {
            return sprintf(
                'Method %s::%s() has parameter $%s with no typehint specified.',
                $methodReflection->getDeclaringClass()->getDisplayName(),
                $methodReflection->getName(),
                $parameterReflection->getName()
            );
        }

        return null;
    }

But I don't see how it would fit in the existing codebase when PR is sent. So I came to ask for ideas/share this snippet for anyone who would like to use it. Feel free to close this, I don't see a way how this could be integrated here in a clean way myself. IMO combining with PHPCS rules is exceeding the responsibility of this library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions