All Projects → phpstan → Phpstan Phpunit

phpstan / Phpstan Phpunit

Licence: mit
PHPUnit extensions and rules for PHPStan

Projects that are alternatives of or similar to Phpstan Phpunit

Revive
🔥 ~6x faster, stricter, configurable, extensible, and beautiful drop-in replacement for golint
Stars: ✭ 3,139 (+1170.85%)
Mutual labels:  static-analysis, static-code-analysis
Warnings Ng Plugin
Jenkins Warnings Plugin - Next Generation
Stars: ✭ 248 (+0.4%)
Mutual labels:  static-analysis, static-code-analysis
Perl Critic
The leading static analyzer for Perl. Configurable, extensible, powerful.
Stars: ✭ 149 (-39.68%)
Mutual labels:  static-analysis, static-code-analysis
Phpstan
PHP Static Analysis Tool - discover bugs in your code without running it!
Stars: ✭ 10,534 (+4164.78%)
Mutual labels:  static-analysis, static-code-analysis
Codeclimate
Code Climate CLI
Stars: ✭ 2,273 (+820.24%)
Mutual labels:  static-analysis, static-code-analysis
Abaplint
Standalone linter for ABAP
Stars: ✭ 111 (-55.06%)
Mutual labels:  static-analysis, static-code-analysis
Coveragechecker
Allows old code to use new standards
Stars: ✭ 159 (-35.63%)
Mutual labels:  static-code-analysis, phpunit
Phpinspectionsea
A Static Code Analyzer for PHP (a PhpStorm/Idea Plugin)
Stars: ✭ 1,211 (+390.28%)
Mutual labels:  static-analysis, static-code-analysis
Pyt
A Static Analysis Tool for Detecting Security Vulnerabilities in Python Web Applications
Stars: ✭ 2,061 (+734.41%)
Mutual labels:  static-analysis, static-code-analysis
Infer
A static analyzer for Java, C, C++, and Objective-C
Stars: ✭ 12,823 (+5091.5%)
Mutual labels:  static-analysis, static-code-analysis
Larastan
⚗️ Adds code analysis to Laravel improving developer productivity and code quality.
Stars: ✭ 3,554 (+1338.87%)
Mutual labels:  static-analysis, static-code-analysis
Forbidden Apis
Policeman's Forbidden API Checker
Stars: ✭ 216 (-12.55%)
Mutual labels:  static-analysis, static-code-analysis
Unimport
A linter, formatter for finding and removing unused import statements.
Stars: ✭ 96 (-61.13%)
Mutual labels:  static-analysis, static-code-analysis
I18n Tasks
Manage translation and localization with static analysis, for Ruby i18n
Stars: ✭ 1,748 (+607.69%)
Mutual labels:  static-analysis, static-code-analysis
Pest
🐞 Primitive Erlang Security Tool
Stars: ✭ 79 (-68.02%)
Mutual labels:  static-analysis, static-code-analysis
Cflint
Static code analysis for CFML (a linter)
Stars: ✭ 156 (-36.84%)
Mutual labels:  static-analysis, static-code-analysis
Flake8
The official GitHub mirror of https://gitlab.com/pycqa/flake8
Stars: ✭ 1,112 (+350.2%)
Mutual labels:  static-analysis, static-code-analysis
Static Analysis
⚙️ A curated list of static analysis (SAST) tools for all programming languages, config files, build tools, and more.
Stars: ✭ 9,310 (+3669.23%)
Mutual labels:  static-analysis, static-code-analysis
Phpstan Deprecation Rules
PHPStan rules for detecting usage of deprecated classes, methods, properties, constants and traits.
Stars: ✭ 160 (-35.22%)
Mutual labels:  static-analysis, static-code-analysis
Spotbugs
SpotBugs is FindBugs' successor. A tool for static analysis to look for bugs in Java code.
Stars: ✭ 2,569 (+940.08%)
Mutual labels:  static-analysis, static-code-analysis

PHPStan PHPUnit extensions and rules

Build Latest Stable Version License

This extension provides following features:

  • createMock(), getMockForAbstractClass() and getMockFromWsdl() methods return an intersection type (see the detailed explanation of intersection types) of the mock object and the mocked class so that both methods from the mock object (like expects) and from the mocked class are available on the object.
  • getMock() called on MockBuilder is also supported.
  • Interprets Foo|PHPUnit_Framework_MockObject_MockObject in phpDoc so that it results in an intersection type instead of a union type.
  • Defines early terminating method calls for the PHPUnit\Framework\TestCase class to prevent undefined variable errors.
  • Specifies types of expressions passed to various assert methods like assertInstanceOf, assertTrue, assertInternalType etc.
  • Combined with PHPStan's level 4, it points out always-true and always-false asserts like assertTrue(true) etc.

It also contains this strict framework-specific rules (can be enabled separately):

  • Check that you are not using assertSame() with true as expected value. assertTrue() should be used instead.
  • Check that you are not using assertSame() with false as expected value. assertFalse() should be used instead.
  • Check that you are not using assertSame() with null as expected value. assertNull() should be used instead.
  • Check that you are not using assertSame() with count($variable) as second parameter. assertCount($variable) should be used instead.

How to document mock objects in phpDocs?

If you need to configure the mock even after you assign it to a property or return it from a method, you should add PHPUnit_Framework_MockObject_MockObject to the phpDoc:

/**
 * @return Foo&PHPUnit_Framework_MockObject_MockObject
 */
private function createFooMock()
{
	return $this->createMock(Foo::class);
}

public function testSomething()
{
	$fooMock = $this->createFooMock();
	$fooMock->method('doFoo')->will($this->returnValue('test'));
	$fooMock->doFoo();
}

Please note that the correct syntax for intersection types is Foo&PHPUnit_Framework_MockObject_MockObject. Foo|PHPUnit_Framework_MockObject_MockObject is also supported, but only for ecosystem and legacy reasons.

If the mock is fully configured and only the methods of the mocked class are supposed to be called on the value, it's fine to typehint only the mocked class:

/** @var Foo */
private $foo;

protected function setUp()
{
	$fooMock = $this->createMock(Foo::class);
	$fooMock->method('doFoo')->will($this->returnValue('test'));
	$this->foo = $fooMock;
}

public function testSomething()
{
	$this->foo->doFoo();
	// $this->foo->method() and expects() can no longer be called
}

Installation

To use this extension, require it in Composer:

composer require --dev phpstan/phpstan-phpunit

If you also install phpstan/extension-installer then you're all set!

Manual installation

If you don't want to use phpstan/extension-installer, include extension.neon in your project's PHPStan config:

includes:
    - vendor/phpstan/phpstan-phpunit/extension.neon

To perform framework-specific checks, include also this file:

    - vendor/phpstan/phpstan-phpunit/rules.neon
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].