Features PhpStorm Tutorials

PhpStorm Type Inference and Mocking Frameworks

Recently, we received a question on Twitter asking about how PhpStorm handles type inference when working with mocking frameworks. Imagine having the following two classes:

When writing a unit test for ClassA, it’s very easy to mock ClassB using PHPUnit’s mocking capabilities. By mocking ClassB, you can mold an instance of ClassB and add additional validations on it without having to modify the code for ClassB. For example, you might want to validate that there’s only one call being made to ClassB’s bar() method.

PhpStorm gives you code completion on PHPUnit so it’s really easy to setup a mock object for ClassB:

By default, PhpStorm is capable of figuring out the available methods on the mock object. However, it only displays those for PHPUnit’s PHPUnit_Framework_MockObject_MockObject class. Fortunately, we can solve this by instructing PhpStorm to infer type information from other classes as well, by using a simple docblock comment. Adding such comment can be done using PhpStorm’s “Add PHPDoc comment” intention (ALT+Enter on the variable):

If we only want to have type information for ClassB, we should use:

To have type information for multiple classes, ClassB and PHPUnit’s mock object, we can use:

A little trick that makes your development a lot easier. Enjoy!

Develop with pleasure!
– JetBrains Web IDE Team

Comments below can no longer be edited.

7 Responses to PhpStorm Type Inference and Mocking Frameworks

  1. Avatar

    Daniel Kullik says:

    January 9, 2013

    Is there any difference between /** @var TYPE NAME */ and /** @var NAME TYPE */ for variables? Both seem to work in PhpStorm.


  2. Avatar

    Tim Glabisch says:

    January 9, 2013

    a bit bad is that you cant do thinks like /** @this ZEND_VIEW */

    is there a plan to extend the type inference? i would like to see something like:

    /** @returns `arguments[0].replace(‘_’, ‘/’)`
    function service_locator($…) { }

    this would be amazing for something like di’s

    • Avatar

      dex says:

      January 9, 2013

      @Tim: Regarding first part of your comment… Do you want to get code assistance e.g. in your Zend view scripts? If so, it works well with /** @var $this Zend_View */ for me.

    • Avatar

      Andriy Bazanov says:

      January 10, 2013

      Sounds like a bad idea to me. PHPDoc comments should really follow the standards and not re-inventing something completely different (like proposed example) for standard PHPDoc tags.

      In any case — I think this ticket is what you are after: http://youtrack.jetbrains.com/issue/WI-6027

  3. Avatar

    Maarten Balliauw says:

    January 9, 2013

    @Daniel PhpStorm indeed supports both. The standard is /** @var TYPE NAME */ but a lot of libraries use the /** @var NAME TYPE */ syntax.

  4. Avatar

    kris says:

    January 9, 2013

    Just a little suggestion.

    I have seen that if I do CI and use phpdesigner then they can make me able to call auth->signin() through autocomplete phpstorm not have done it.

    biggest problem for me is Performance. I still not feel good even I have 8GB.

  5. Avatar

    Richard Tuin says:

    January 18, 2013

    This is a great feature. In which version of PHPStorm was this introduced?

Discover more