Skip to content

Commit

Permalink
Maintenance release
Browse files Browse the repository at this point in the history
- Switch from roots/wordpress to johnpbloch/wordpress-core
- Update Psalm
- Improve PHPCS checks  (fix testVersion, run for tests, check PSR-4)
- Remove hook callbacks when not needed anymore
  • Loading branch information
gmazzap committed Mar 9, 2021
1 parent 63284de commit b059e34
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .psalm/autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
return;
}

define('ABSPATH', './vendor/roots/wordpress/');
define('ABSPATH', './vendor/johnpbloch/wordpress-core/');
define('WPINC', 'wp-includes');
define('WP_CONTENT_DIR', ABSPATH . 'wp-content');

Expand Down
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
"require-dev": {
"phpunit/phpunit": "~7.5.20 || ^8",
"inpsyde/php-coding-standards": "^1",
"vimeo/psalm": "^3.14.2",
"vimeo/psalm": "@stable",
"mockery/mockery": "~1.3.3",
"brain/monkey": "^2.4.2",
"roots/wordpress": "^5.5.1"
"johnpbloch/wordpress-core": "@stable"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -55,7 +55,6 @@
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
},
"wordpress-install-dir": "vendor/roots/wordpress"
}
}
}
12 changes: 11 additions & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@
<ruleset>

<file>./src/</file>
<file>./tests/</file>

<arg value="sp"/>
<arg name="colors"/>
<config name="testVersion" value="7.2-"/>
<config name="testVersion" value="7.1-"/>

<rule ref="Inpsyde"/>

<rule ref="Inpsyde.CodeQuality.Psr4">
<properties>
<property
name="psr4"
type="array"
value="Inpsyde=>src,Inpsyde\Tests=>tests"/>
</properties>
</rule>
</ruleset>
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@

<issueHandlers>
<MixedAssignment errorLevel="suppress" />
<RedundantCastGivenDocblockType errorLevel="suppress" />
</issueHandlers>
</psalm>
23 changes: 15 additions & 8 deletions src/WpContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ final class WpContext implements \JsonSerializable
*/
public static function new(): WpContext
{
return new static(array_fill_keys(self::ALL, false));
return new self(array_fill_keys(self::ALL, false));
}

/**
Expand All @@ -70,9 +70,11 @@ public static function determine(): WpContext
// When nothing else matches, we assume it is a front-office request.
$isFront = $undetermined && !$isRest && !$isLogin;

// Note that when core is installing **only** `INSTALLING` will be true, not even `CORE`.
// This is done to do as less as possible during installation, when most of WP does not act
// as expected.
/*
* Note that when core is installing **only** `INSTALLING` will be true, not even `CORE`.
* This is done to do as less as possible during installation, when most of WP does not act
* as expected.
*/

$instance = new self(
[
Expand Down Expand Up @@ -107,9 +109,11 @@ private static function isRestRequest(): bool
return !empty($_GET['rest_route']); // phpcs:ignore
}

// This is needed because, if called early, global $wp_rewrite is not defined but required
// by get_rest_url(). WP will reuse what we set here, or in worst case will replace, but no
// consequences for us in any case.
/*
* This is needed because, if called early, global $wp_rewrite is not defined but required
* by get_rest_url(). WP will reuse what we set here, or in worst case will replace, but no
* consequences for us in any case.
*/
if (empty($GLOBALS['wp_rewrite'])) {
$GLOBALS['wp_rewrite'] = new \WP_Rewrite();
}
Expand Down Expand Up @@ -282,7 +286,7 @@ public function isInstalling(): bool
/**
* @return array
*/
public function jsonSerialize()
public function jsonSerialize(): array
{
return $this->data;
}
Expand Down Expand Up @@ -328,6 +332,7 @@ private function removeActionHooks(): void
foreach ($this->actionCallbacks as $action => $callback) {
remove_action($action, $callback, PHP_INT_MIN);
}
$this->actionCallbacks = [];
}

/**
Expand All @@ -341,5 +346,7 @@ private function resetAndForce(string $context): void
$this->data[self::CORE] = true;
$this->data[self::CLI] = $cli;
$this->data[$context] = true;

$this->removeActionHooks();
}
}
21 changes: 12 additions & 9 deletions tests/WpContextTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php # -*- coding: utf-8 -*-
<?php

declare(strict_types=1);

Expand Down Expand Up @@ -28,9 +28,11 @@ protected function setUp(): void
{
parent::setUp();
Monkey\setUp();
Monkey\Functions\expect('add_query_arg')->with([])->andReturnUsing(function (): string {
return $this->currentPath;
});
Monkey\Functions\expect('add_query_arg')
->with([])
->andReturnUsing(function (): string {
return $this->currentPath;
});
}

/**
Expand Down Expand Up @@ -105,7 +107,7 @@ public function testIsLoginLate(): void

$onLoginInit = null;
Monkey\Actions\expectAdded('login_init')
->whenHappen(function (callable $callback) use (&$onLoginInit) {
->whenHappen(static function (callable $callback) use (&$onLoginInit) {
$onLoginInit = $callback;
});

Expand Down Expand Up @@ -161,7 +163,7 @@ public function testIsRestLate(): void

$onRestInit = null;
Monkey\Actions\expectAdded('rest_api_init')
->whenHappen(function (callable $callback) use (&$onRestInit) {
->whenHappen(static function (callable $callback) use (&$onRestInit) {
$onRestInit = $callback;
});

Expand Down Expand Up @@ -364,8 +366,9 @@ private function mockIsLoginRequest(bool $is): void
{
$is and $this->currentPath = '/wp-login.php';
Monkey\Functions\when('wp_login_url')->justReturn('https://example.com/wp-login.php');
Monkey\Functions\when('home_url')->alias(static function (string $path = ''): string {
return 'https://example.com/' . ltrim($path, '/');
});
Monkey\Functions\when('home_url')
->alias(static function (string $path = ''): string {
return 'https://example.com/' . ltrim($path, '/');
});
}
}

0 comments on commit b059e34

Please sign in to comment.