Skip to content

Add PHPstan to CI. #1466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .distignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ npm-debug.log
package-lock.json
package.json
phpcs.xml
phpstan.neon.dist
phpunit.xml
phpunit.xml.dist
README.md
Expand Down
4 changes: 4 additions & 0 deletions .github/changelog/add-ci-phpstan
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

Add PHPstan to CI
48 changes: 48 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: PHPstan
on:
push:
branches:
- trunk
pull_request:
jobs:
phpstan:
runs-on: ubuntu-latest
strategy:
matrix:
php-version: ['7.4', '8.2', '8.4']
name: PHPStan for WordPress ${{ matrix.php-version }}
steps:
- name: Install svn
run: |
sudo apt-get update
sudo apt-get install subversion

- name: Checkout
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
coverage: none
tools: composer, phpunit-polyfills, cs2pr
extensions: dom, iconv, json, libxml, zip

- name: Install Composer dependencies for PHP
uses: ramsey/composer-install@v3

- name: Log debug information
run: |
git --version
php --version
composer --version

- name: Install third party plugins, so that PHP Stan finds that functions
if: steps.cache-wordpress.outputs.cache-hit != 'true'
run: bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1 latest true false false true

- name: Running PHPStan Analyze
if: ${{ success() || failure() }}
run: |
./vendor/bin/phpstan --version
./vendor/bin/phpstan analyze -vv --memory-limit=2G --error-format=checkstyle | cs2pr
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"dealerdirect/phpcodesniffer-composer-installer": "^1.0.0",
"sirbrillig/phpcs-variable-analysis": "^2.11",
"phpcsstandards/phpcsextra": "^1.1.0",
"dms/phpunit-arraysubset-asserts": "^0.5.0"
"dms/phpunit-arraysubset-asserts": "^0.5.0",
"szepeviktor/phpstan-wordpress": "^2.0"
},
"config": {
"allow-plugins": true
Expand Down
14 changes: 7 additions & 7 deletions includes/activity/class-generic-object.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ public function to_array( $include_json_ld_context = true ) {
/**
* Filter the array of the ActivityPub object.
*
* @param array $array The array of the ActivityPub object.
* @param string $class The class of the ActivityPub object.
* @param int $id The ID of the ActivityPub object.
* @param Base_Object $object The ActivityPub object.
* @param array $array The array of the ActivityPub object.
* @param string $class The class of the ActivityPub object.
* @param string $id The ID of the ActivityPub object.
* @param Generic_Object $object The ActivityPub object.
*
* @return array The filtered array of the ActivityPub object.
*/
Expand All @@ -275,9 +275,9 @@ public function to_array( $include_json_ld_context = true ) {
/**
* Filter the array of the ActivityPub object by class.
*
* @param array $array The array of the ActivityPub object.
* @param int $id The ID of the ActivityPub object.
* @param Base_Object $object The ActivityPub object.
* @param array $array The array of the ActivityPub object.
* @param string $id The ID of the ActivityPub object.
* @param Generic_Object $object The ActivityPub object.
*
* @return array The filtered array of the ActivityPub object.
*/
Expand Down
6 changes: 4 additions & 2 deletions includes/class-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ public function post( $args ) {
switch ( $args[0] ) {
case 'delete':
WP_CLI::confirm( 'Do you really want to delete the (Custom) Post with the ID: ' . $args[1] );
Post::schedule_post_activity( 'trash', 'publish', $post );
$updated = clone $post;
$updated->post_status = 'trash';
Post::schedule_post_activity( $updated->ID, $updated, false, $post );
WP_CLI::success( '"Delete" activity is queued.' );
break;
case 'update':
Post::schedule_post_activity( 'publish', 'publish', $post );
Post::schedule_post_activity( $post->ID, $post, true, $post );
WP_CLI::success( '"Update" activity is queued.' );
break;
default:
Expand Down
2 changes: 1 addition & 1 deletion includes/class-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function init() {

\add_filter( 'comment_reply_link', array( self::class, 'comment_reply_link' ), 10, 3 );
\add_filter( 'comment_class', array( self::class, 'comment_class' ), 10, 3 );
\add_filter( 'get_comment_link', array( self::class, 'remote_comment_link' ), 11, 3 );
\add_filter( 'get_comment_link', array( self::class, 'remote_comment_link' ), 11, 2 );
\add_action( 'wp_enqueue_scripts', array( self::class, 'enqueue_scripts' ) );
\add_action( 'pre_get_comments', array( static::class, 'comment_query' ) );
\add_filter( 'pre_comment_approved', array( static::class, 'pre_comment_approved' ), 10, 2 );
Expand Down
4 changes: 2 additions & 2 deletions includes/class-notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ public function send() {
/**
* Action to send ActivityPub notifications.
*
* @param Notification $this The notification object.
* @param Notification $instance The notification object.
*/
do_action( 'activitypub_notification', $this );

/**
* Type-specific action to send ActivityPub notifications.
*
* @param Notification $this The notification object.
* @param Notification $instance The notification object.
*/
do_action( "activitypub_notification_{$type}", $this );
}
Expand Down
2 changes: 1 addition & 1 deletion includes/collection/class-extra-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static function get_actor_fields( $user_id ) {
*/
public static function get_formatted_content( $post ) {
$content = \get_the_content( null, false, $post );
$content = Link::the_content( $content, true );
$content = Link::the_content( $content );
if ( site_supports_blocks() ) {
$content = \do_blocks( $content );
}
Expand Down
8 changes: 4 additions & 4 deletions includes/collection/class-interactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Interactions {
*
* @param array $activity The activity-object.
*
* @return array|false The comment data or false on failure.
* @return int|false|\WP_Error The comment ID or false or WP_Error on failure.
*/
public static function add_comment( $activity ) {
$commentdata = self::activity_to_comment( $activity );
Expand Down Expand Up @@ -297,7 +297,7 @@ public static function activity_to_comment( $activity ) {
*/
public static function persist( $commentdata, $action = self::INSERT ) {
// Disable flood control.
\remove_action( 'check_comment_flood', 'check_comment_flood_db', 10 );
\remove_action( 'check_comment_flood', 'check_comment_flood_db' );
// Do not require email for AP entries.
\add_filter( 'pre_option_require_name_email', '__return_false' );
// No nonce possible for this submission route.
Expand All @@ -315,10 +315,10 @@ function () {
$state = \wp_update_comment( $commentdata, true );
}

\remove_filter( 'wp_kses_allowed_html', array( self::class, 'allowed_comment_html' ), 10 );
\remove_filter( 'wp_kses_allowed_html', array( self::class, 'allowed_comment_html' ) );
\remove_filter( 'pre_option_require_name_email', '__return_false' );
// Restore flood control.
\add_action( 'check_comment_flood', 'check_comment_flood_db', 10, 4 );
\add_action( 'check_comment_flood', 'check_comment_flood_db' );

if ( 1 === $state ) {
return $commentdata;
Expand Down
7 changes: 4 additions & 3 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,6 @@ function set_wp_object_state( $wp_object, $state ) {
* Allow plugins to mark WordPress objects as federated.
*
* @param \WP_Comment|\WP_Post $wp_object The WordPress object.
* @param string $state The state of the object.
*/
\apply_filters( 'activitypub_mark_wp_object_as_federated', $wp_object );
}
Expand All @@ -825,6 +824,7 @@ function get_wp_object_state( $wp_object ) {
/**
* Allow plugins to get the federation state of a WordPress object.
*
* @param false $state The state of the object.
* @param \WP_Comment|\WP_Post $wp_object The WordPress object.
*/
return \apply_filters( 'activitypub_get_wp_object_state', false, $wp_object );
Expand Down Expand Up @@ -862,8 +862,9 @@ function get_post_type_description( $post_type ) {
/**
* Allow plugins to get the description of a post type.
*
* @param string $description The description of the post type.
* @param \WP_Post_Type $post_type The post type object.
* @param string $description The description of the post type.
* @param string $post_type_name The post type name.
* @param \WP_Post_Type $post_type The post type object.
*/
return apply_filters( 'activitypub_post_type_description', $description, $post_type->name, $post_type );
}
Expand Down
14 changes: 8 additions & 6 deletions includes/handler/class-announce.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Activitypub\Handler;

use Activitypub\Activity\Activity;
use Activitypub\Http;
use Activitypub\Comment;
use Activitypub\Collection\Interactions;
Expand Down Expand Up @@ -65,18 +66,19 @@ public static function handle_announce( $announcement, $user_id, $activity = nul
/**
* Fires after an Announce has been received.
*
* @param array $object The object.
* @param int $user_id The id of the local blog-user.
* @param array $activity The activity object.
* @param array $object The object.
* @param int $user_id The id of the local blog-user.
* @param string $type The type of the activity.
* @param Activity|null $activity The activity object.
*/
\do_action( 'activitypub_inbox', $object, $user_id, $type, $activity );

/**
* Fires after an Announce of a specific type has been received.
*
* @param array $object The object.
* @param int $user_id The id of the local blog-user.
* @param array $activity The activity object.
* @param array $object The object.
* @param int $user_id The id of the local blog-user.
* @param Activity|null $activity The activity object.
*/
\do_action( "activitypub_inbox_{$type}", $object, $user_id, $activity );
}
Expand Down
1 change: 0 additions & 1 deletion includes/handler/class-create.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public static function handle_create( $activity, $user_id, $activity_object = nu
*
* @param array $activity The activity-object.
* @param int $user_id The id of the local blog-user.
* @param \WP_Comment|\WP_Error $check_dupe The comment object or WP_Error.
* @param \Activitypub\Activity\Activity $activity_object The activity object.
*/
\do_action( 'activitypub_inbox_update', $activity, $user_id, $activity_object );
Expand Down
2 changes: 1 addition & 1 deletion includes/handler/class-like.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Like {
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
\add_action( 'activitypub_inbox_like', array( self::class, 'handle_like' ), 10, 3 );
\add_action( 'activitypub_inbox_like', array( self::class, 'handle_like' ), 10, 2 );
\add_filter( 'activitypub_get_outbox_activity', array( self::class, 'outbox_activity' ) );
}

Expand Down
2 changes: 1 addition & 1 deletion includes/model/class-user.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static function from_wp_user( $user_id ) {
);
}

$object = new static();
$object = new self();
$object->_id = $user_id;

return $object;
Expand Down
15 changes: 8 additions & 7 deletions includes/rest/class-actors-inbox-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Activitypub\Rest;

use Activitypub\Activity\Activity;
use Activitypub\Activity\Generic_Object;
use Activitypub\Collection\Actors;

use function Activitypub\get_context;
Expand Down Expand Up @@ -196,19 +197,19 @@ public function create_item( $request ) {
/**
* ActivityPub inbox action.
*
* @param array $data The data array.
* @param int|null $user_id The user ID.
* @param string $type The type of the activity.
* @param Activity $activity The Activity object.
* @param array $data The data array.
* @param int|null $user_id The user ID.
* @param string $type The type of the activity.
* @param Generic_Object|\WP_Error $activity The Activity object.
*/
\do_action( 'activitypub_inbox', $data, $user->get__id(), $type, $activity );

/**
* ActivityPub inbox action for specific activity types.
*
* @param array $data The data array.
* @param int|null $user_id The user ID.
* @param Activity $activity The Activity object.
* @param array $data The data array.
* @param int|null $user_id The user ID.
* @param Generic_Object|\WP_Error $activity The Activity object.
*/
\do_action( 'activitypub_inbox_' . $type, $data, $user->get__id(), $activity );

Expand Down
15 changes: 8 additions & 7 deletions includes/rest/class-inbox-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Activitypub\Rest;

use Activitypub\Activity\Activity;
use Activitypub\Activity\Generic_Object;

/**
* Inbox_Controller class.
Expand Down Expand Up @@ -133,19 +134,19 @@ public function create_item( $request ) {
/**
* ActivityPub inbox action.
*
* @param array $data The data array.
* @param int|null $user_id The user ID.
* @param string $type The type of the activity.
* @param Activity $activity The Activity object.
* @param array $data The data array.
* @param int|null $user_id The user ID.
* @param string $type The type of the activity.
* @param Generic_Object|\WP_Error $activity The Activity object.
*/
\do_action( 'activitypub_inbox', $data, null, $type, $activity );

/**
* ActivityPub inbox action for specific activity types.
*
* @param array $data The data array.
* @param int|null $user_id The user ID.
* @param Activity $activity The Activity object.
* @param array $data The data array.
* @param int|null $user_id The user ID.
* @param Generic_Object|\WP_Error $activity The Activity object.
*/
\do_action( 'activitypub_inbox_' . $type, $data, null, $activity );

Expand Down
2 changes: 1 addition & 1 deletion includes/rest/class-interaction-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function register_routes() {
*/
public function get_item( $request ) {
$uri = $request->get_param( 'uri' );
$redirect_url = null;
$redirect_url = '';
$object = Http::get_remote_object( $uri );

if ( \is_wp_error( $object ) || ! isset( $object['type'] ) ) {
Expand Down
4 changes: 2 additions & 2 deletions includes/rest/class-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ public static function validate_requests( $response, $handler, $request ) {
/**
* Modify the parameter priority order for a REST API request.
*
* @param string[] $order Array of types to check, in order of priority.
* @param WP_REST_Request $request The request object.
* @param string[] $order Array of types to check, in order of priority.
* @param \WP_REST_Request $request The request object.
*
* @return string[] The modified order of types to check.
*/
Expand Down
22 changes: 11 additions & 11 deletions includes/scheduler/class-actor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ class Actor {
public static function init() {
// Profile updates for blog options.
if ( ! is_user_type_disabled( 'blog' ) ) {
\add_action( 'update_option_site_icon', array( self::class, 'blog_user_update' ) );
\add_action( 'update_option_blogdescription', array( self::class, 'blog_user_update' ) );
\add_action( 'update_option_blogname', array( self::class, 'blog_user_update' ) );
\add_action( 'add_option_activitypub_header_image', array( self::class, 'blog_user_update' ) );
\add_action( 'update_option_activitypub_header_image', array( self::class, 'blog_user_update' ) );
\add_action( 'add_option_activitypub_blog_identifier', array( self::class, 'blog_user_update' ) );
\add_action( 'update_option_activitypub_blog_identifier', array( self::class, 'blog_user_update' ) );
\add_action( 'add_option_activitypub_blog_description', array( self::class, 'blog_user_update' ) );
\add_action( 'update_option_activitypub_blog_description', array( self::class, 'blog_user_update' ) );
\add_filter( 'update_option_site_icon', array( self::class, 'blog_user_update' ) );
\add_filter( 'update_option_blogdescription', array( self::class, 'blog_user_update' ) );
\add_filter( 'update_option_blogname', array( self::class, 'blog_user_update' ) );
\add_filter( 'add_option_activitypub_header_image', array( self::class, 'blog_user_update' ) );
\add_filter( 'update_option_activitypub_header_image', array( self::class, 'blog_user_update' ) );
\add_filter( 'add_option_activitypub_blog_identifier', array( self::class, 'blog_user_update' ) );
\add_filter( 'update_option_activitypub_blog_identifier', array( self::class, 'blog_user_update' ) );
\add_filter( 'add_option_activitypub_blog_description', array( self::class, 'blog_user_update' ) );
\add_filter( 'update_option_activitypub_blog_description', array( self::class, 'blog_user_update' ) );
\add_filter( 'pre_set_theme_mod_custom_logo', array( self::class, 'blog_user_update' ) );
\add_filter( 'pre_set_theme_mod_header_image', array( self::class, 'blog_user_update' ) );
}
Expand All @@ -44,8 +44,8 @@ public static function init() {
// @todo figure out a feasible way of updating the header image since it's not unique to any user.
}

\add_action( 'add_option_activitypub_actor_mode', array( self::class, 'blog_user_update' ) );
\add_action( 'update_option_activitypub_actor_mode', array( self::class, 'blog_user_update' ) );
\add_filter( 'add_option_activitypub_actor_mode', array( self::class, 'blog_user_update' ) );
\add_filter( 'update_option_activitypub_actor_mode', array( self::class, 'blog_user_update' ) );

\add_action( 'transition_post_status', array( self::class, 'schedule_post_activity' ), 33, 3 );
}
Expand Down
Loading
Loading