Skip to content

phph 7.4 typed hinted properties and mocks #75

@nikophil

Description

@nikophil

Hello,

here is a problematic snippet in php 7.4:

<?php

class MyTest extends TestCase

    private Object $testedObject
    private $someMockedObject;

    public function setUp(): void
    {
         $this->testedObject = new Object();
         $this->someMockedObject = $this->createMock(SomeInterface::class);
    }

    public function testItDoesSomething(): void
    {
        $this->someMockedObject->expects()...

        $this->testedObject->doSomething($this->someMockedObject);
    }

First, at level 6, phpstan complains about the property which has no typehint

Property MyTest::$someMockedObject has no typehint specified

Event if we put /* @var SomeInterface|MockObject */ it still complains.

Thus, we have to chose a type hint between MockObject or SomeInterface but it complains one way like the other because expects() method is unknown on SomeInterface or Object::doSomething() parameter 1 expects an instance of SomeInterface

any clue how i can make this pass?

thanks :)

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