Skip to content

Commit

Permalink
Merge pull request #29 from weirdan/psalm-5-compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan authored Nov 25, 2022
2 parents eb11159 + a141d96 commit 843d545
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
runs-on: ubuntu-20.04
strategy:
matrix:
php: ['7.3', '7.4', '8.0']
php: ['7.4', '8.0']
deps: ['lowest', 'highest']
fail-fast: false
steps:
Expand Down
11 changes: 9 additions & 2 deletions Hooks/MockReturnTypeUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,15 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve
if ($fq_class_name) {
foreach ($return_type_candidate->getAtomicTypes() as $return_atomic_type) {
if ($return_atomic_type instanceof Type\Atomic\TNamedObject) {
$return_atomic_type->value = 'Mockery\MockInterface';
$return_atomic_type->addIntersectionType(new Type\Atomic\TNamedObject($fq_class_name));
$new_return_type = $return_type_candidate->getBuilder();
$new_return_type->removeType($return_atomic_type->getId());

$return_atomic_type = $return_atomic_type
->setValue('Mockery\MockInterface')
->addIntersectionType(new Type\Atomic\TNamedObject($fq_class_name))
;

$event->setReturnTypeCandidate($new_return_type->addType($return_atomic_type)->freeze());
break;
}
}
Expand Down
16 changes: 13 additions & 3 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ public function __invoke(RegistrationInterface $registration, SimpleXMLElement $
{
$registration->addStubFile(__DIR__ . '/stubs/Mockery.php');

$this->loadPhpUnitIntegration($registration);

require_once __DIR__ . '/Hooks/MockReturnTypeUpdater.php';
$registration->registerHooksFromClass(Hooks\MockReturnTypeUpdater::class);
}

private function loadPhpUnitIntegration(RegistrationInterface $registration): void
{
// Mockery doesn't do any funny stuff with trait aliases since v1.4
if (VersionUtils::packageVersionIs('mockery/mockery', '>=', '1.4')) {
return;
}

if (
class_exists('PHPUnit_Framework_TestCase')
|| (class_exists('\PHPUnit\Runner\Version')
Expand All @@ -25,8 +38,5 @@ class_exists('\PHPUnit\Runner\Version')
) {
$registration->addStubFile(__DIR__ . '/stubs/Mockery/AssertPostConditionsV8.php');
}

require_once __DIR__ . '/Hooks/MockReturnTypeUpdater.php';
$registration->registerHooksFromClass(Hooks\MockReturnTypeUpdater::class);
}
}
34 changes: 34 additions & 0 deletions VersionUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Psalm\MockeryPlugin;

use Composer\Semver\Comparator;
use Composer\Semver\VersionParser;
use PackageVersions\Versions;

abstract class VersionUtils
{
/** @param "!="|"<"|"<="|"<>"|"="|"=="|">"|">=" $op */
public static function packageVersionIs(string $package, string $op, string $ref): bool
{
try {
/**
* @psalm-suppress RedundantCondition
* @psalm-suppress DeprecatedClass
* @psalm-suppress ArgumentTypeCoercion
* @psalm-suppress RedundantCast
* @psalm-suppress RedundantCondition
*/
$currentVersion = (string) explode('@', Versions::getVersion($package))[0];
} catch (\OutOfBoundsException $exception) {
return false;
}

$parser = new VersionParser();

$currentVersion = $parser->normalize($currentVersion);
$ref = $parser->normalize($ref);

return Comparator::compare($currentVersion, $op, $ref);
}
}
15 changes: 10 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
}
],
"require": {
"mockery/mockery": "^0.9 || ^1.0",
"vimeo/psalm": "dev-master || dev-4.x || ^4.7.1 || ^5@beta"
"php": "~7.4 || ~8.0 || ~8.1 || ~8.2",
"composer/package-versions-deprecated": "^1.10",
"composer/semver": "^1.4 || ^2.0 || ^3.0",
"mockery/mockery": "^1.0",
"vimeo/psalm": "dev-master || ^5.0@rc || ^5.0"
},
"require-dev": {
"codeception/codeception": "^4.0.3",
"codeception/codeception": "^4.1.9",
"squizlabs/php_codesniffer": "^3.3.1",
"weirdan/codeception-psalm-module": "^0.13.1",
"phpunit/phpunit": "^6.0 || ^7.0 || ^8.0"
"phpunit/phpunit": "^9.0"
},
"extra": {
"psalm": {
Expand Down Expand Up @@ -48,6 +51,8 @@
"config": {
"allow-plugins": {
"composer/package-versions-deprecated": true
}
},
"optimize-autoloader": true,
"sort-packages": true
}
}
19 changes: 0 additions & 19 deletions tests/acceptance/PHPUnitIntegration.feature
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,3 @@ Feature: PHPUnitIntegration
And I have the "mockery/mockery" package satisfying the "^1.0"
When I run Psalm
Then I see no errors

Scenario: Plugin stubs don't conflict with Mockery 0.9.x
Given I have the following code
"""
/** @psalm-suppress PropertyNotSetInConstructor */
class MyTestCase extends TestCase
{
/**
* @return void
*/
public function testSomething()
{
$_mock = Mockery::mock(\ArrayAccess::class);
}
}
"""
And I have the "mockery/mockery" package satisfying the "< 1.0"
When I run Psalm
Then I see no errors

0 comments on commit 843d545

Please sign in to comment.