Skip to content

Commit

Permalink
phan: Add stubs for the WordPress.com Editing Toolkit plugin (#36957)
Browse files Browse the repository at this point in the history
A few functions from this plugin are called from Jetpack and the
jetpack-mu-wpcom package.
  • Loading branch information
anomiex authored Apr 17, 2024
1 parent b00a4a0 commit 236ac36
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .phan/config.base.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* - parse_file_list: (array) Files to parse but not analyze. Equivalent to listing in both 'file_list' and 'exclude_analysis_directory_list'.
* - stubs: (array) Predefined stubs to load. Default is `array( 'wordpress', 'wp-cli', 'wpcom' )`.
* - akismet: Stubs from .phan/stubs/akismet-stubs.php.
* - full-site-editing: Stubs from .phan/stubs/full-site-editing-stubs.php.
* - woocommerce: Stubs from php-stubs/woocommerce.
* - woocommerce-internal: Stubs from .phan/stubs/woocommerce-internal-stubs.php.
* - woocommerce-packages: Stubs from php-stubs/woocommerce.
Expand Down Expand Up @@ -57,6 +58,9 @@ function make_phan_config( $dir, $options = array() ) {
case 'akismet':
$stubs[] = "$root/.phan/stubs/akismet-stubs.php";
break;
case 'full-site-editing':
$stubs[] = "$root/.phan/stubs/full-site-editing-stubs.php";
break;
case 'woocommerce':
$stubs[] = "$root/vendor/php-stubs/woocommerce-stubs/woocommerce-stubs.php";
break;
Expand Down
55 changes: 55 additions & 0 deletions .phan/stubs/full-site-editing-stubs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Stubs automatically generated from WordPress.com Editing Toolkit 4.19624
* using the definition file `tools/stubs/full-site-editing-stub-defs.php` in the Jetpack monorepo.
*
* Do not edit this directly! Run tools/stubs/update-stubs.sh to regenerate it.
*/

namespace {
/**
* Limit Global Styles on WP.com to paid plans.
*
* @package full-site-editing-plugin
*/
/**
* Checks if Global Styles should be limited on the given site.
*
* @param int $blog_id Blog ID.
* @return bool Whether Global Styles are limited.
*/
function wpcom_should_limit_global_styles($blog_id = 0)
{
}
/**
* Checks if the current blog has custom styles in use.
*
* @return bool Returns true if custom styles are in use.
*/
function wpcom_global_styles_in_use()
{
}
}
namespace A8C\FSE {
/**
* Whether or not FSE is active.
* If false, FSE functionality should be disabled.
*
* @returns bool True if FSE is active, false otherwise.
* @phan-return mixed Dummy doc for stub.
*/
function is_full_site_editing_active()
{
}
/**
* Whether or not the site is eligible for FSE. This is essentially a feature
* gate to disable FSE on some sites which could theoretically otherwise use it.
*
* By default, sites should not be eligible.
*
* @return bool True if current site is eligible for FSE, false otherwise.
*/
function is_site_eligible_for_full_site_editing()
{
}
}
2 changes: 1 addition & 1 deletion projects/packages/jetpack-mu-wpcom/.phan/baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
return [
// # Issue statistics:
// PhanUndeclaredFunction : 65+ occurrences
// PhanUndeclaredFunction : 60+ occurrences
// PhanTypeMismatchArgumentProbablyReal : 50+ occurrences
// PhanTypeMismatchArgument : 30+ occurrences
// PhanTypeArraySuspicious : 25+ occurrences
Expand Down
1 change: 1 addition & 0 deletions projects/packages/jetpack-mu-wpcom/.phan/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
return make_phan_config(
dirname( __DIR__ ),
array(
'+stubs' => array( 'full-site-editing' ),
'parse_file_list' => array(
"$root/projects/packages/stats-admin/src/class-dashboard.php",
"$root/projects/plugins/jetpack/class-jetpack-stats-dashboard-widget.php",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: added
Comment: Update Phan config to use new WordPress.com Editing Toolkit stubs. No change to functionality.


5 changes: 2 additions & 3 deletions projects/plugins/jetpack/.phan/baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
// PhanTypeInvalidLeftOperandOfNumericOp : 10+ occurrences
// PhanTypeMismatchReturnNullable : 10+ occurrences
// PhanUndeclaredClassProperty : 10+ occurrences
// PhanUndeclaredClassReference : 10+ occurrences
// PhanUndeclaredConstant : 10+ occurrences
// PhanUndeclaredTypeReturnType : 10+ occurrences
// PhanCommentParamWithoutRealParam : 9 occurrences
Expand All @@ -72,6 +71,7 @@
// PhanTypeMismatchArgumentInternalReal : 7 occurrences
// PhanUndeclaredTypeParameter : 7 occurrences
// PhanCommentAbstractOnInheritedMethod : 6 occurrences
// PhanUndeclaredClassReference : 6 occurrences
// PhanUndeclaredTypeProperty : 6 occurrences
// PhanUndeclaredVariableDim : 6 occurrences
// PhanUnextractableAnnotationElementName : 6 occurrences
Expand All @@ -89,7 +89,6 @@
// PhanTypeInvalidRightOperandOfBitwiseOp : 4 occurrences
// PhanTypeInvalidRightOperandOfNumericOp : 4 occurrences
// PhanUndeclaredClassInCallable : 4 occurrences
// PhanUndeclaredClassReference : 4 occurrences
// PhanDeprecatedFunctionInternal : 3 occurrences
// PhanDeprecatedTrait : 3 occurrences
// PhanTypeConversionFromArray : 3 occurrences
Expand Down Expand Up @@ -588,7 +587,7 @@
'sal/class.json-api-post-base.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanRedundantCondition', 'PhanTypeMismatchArgument', 'PhanTypeMismatchReturnNullable', 'PhanTypeMismatchReturnProbablyReal', 'PhanTypePossiblyInvalidDimOffset', 'PhanTypeSuspiciousNonTraversableForeach', 'PhanUndeclaredFunction'],
'sal/class.json-api-site-base.php' => ['PhanParamTooMany', 'PhanRedundantCondition', 'PhanTypeMismatchArgument', 'PhanTypeMismatchReturnProbablyReal', 'PhanUndeclaredFunction'],
'sal/class.json-api-site-jetpack-base.php' => ['PhanRedundantCondition', 'PhanTypeMismatchReturnProbablyReal'],
'sal/class.json-api-site-jetpack.php' => ['PhanParamSignatureMismatch', 'PhanUndeclaredFunction'],
'sal/class.json-api-site-jetpack.php' => ['PhanParamSignatureMismatch'],
'sal/class.json-api-token.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanSuspiciousValueComparison', 'PhanTypeMismatchArgumentProbablyReal', 'PhanTypeMismatchReturn', 'PhanUndeclaredVariable'],
'src/class-jetpack-crm-data.php' => ['PhanUndeclaredFunction'],
'src/class-jetpack-modules-overrides.php' => ['PhanRedundantCondition'],
Expand Down
2 changes: 1 addition & 1 deletion projects/plugins/jetpack/.phan/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
return make_phan_config(
dirname( __DIR__ ),
array(
'+stubs' => array( 'akismet', 'woocommerce', 'woocommerce-internal', 'woocommerce-packages' ),
'+stubs' => array( 'akismet', 'full-site-editing', 'woocommerce', 'woocommerce-internal', 'woocommerce-packages' ),
'exclude_file_list' => array(
// Mocks of core classes.
'tests/php/_inc/lib/mocks/class-simplepie-file.php',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: Update Phan config to use new WordPress.com Editing Toolkit stubs. No change to functionality.


39 changes: 39 additions & 0 deletions tools/stubs/full-site-editing-stub-defs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Stub config for WordPress.com Editing Toolkit classes and such needed in the Jetpack monorepo.
*
* @package automattic/jetpack-monorepo
*/

// phpcs:disable PHPCompatibility.Syntax.NewFlexibleHeredocNowdoc.ClosingMarkerNoNewLine -- https://github.com/PHPCompatibility/PHPCompatibility/issues/1696

$work_dir = getenv( 'WORK_DIR' );
if ( ! is_dir( $work_dir ) ) {
throw new RuntimeException( 'WORK_DIR is not set or does not refer to a directory' );
}

$data = file_get_contents( "$work_dir/full-site-editing/full-site-editing-plugin.php" );
if ( ! preg_match( '/^ \* Version: (\d+\.\d+.*)/m', (string) $data, $m ) ) {
throw new RuntimeException( "Failed to extract version from $work_dir/full-site-editing/full-site-editing-plugin.php" );
}
$version = $m[1];

return array(
'header' => <<<HEAD
/**
* Stubs automatically generated from WordPress.com Editing Toolkit $version
* using the definition file `tools/stubs/full-site-editing-stub-defs.php` in the Jetpack monorepo.
*
* Do not edit this directly! Run tools/stubs/update-stubs.sh to regenerate it.
*/
HEAD,
'basedir' => "$work_dir/full-site-editing/",
'files' => array(
'wpcom-global-styles/index.php' => array(
'function' => array( 'wpcom_global_styles_in_use', 'wpcom_should_limit_global_styles' ),
),
'dotcom-fse/helpers.php' => array(
'function' => array( 'A8C\FSE\is_full_site_editing_active', 'A8C\FSE\is_site_eligible_for_full_site_editing' ),
),
),
);
8 changes: 8 additions & 0 deletions tools/stubs/update-stubs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ echo
info 'Extracting Akismet stubs'
"$BASE/projects/packages/stub-generator/bin/jetpack-stub-generator" --output "$BASE/.phan/stubs/akismet-stubs.php" "$BASE/tools/stubs/akismet-stub-defs.php"

echo
info 'Downloading WordPress.com Editing Toolkit'
fetch_plugin full-site-editing

echo
info 'Extracting WordPress.com Editing Toolkit stubs'
"$BASE/projects/packages/stub-generator/bin/jetpack-stub-generator" --output "$BASE/.phan/stubs/full-site-editing-stubs.php" "$BASE/tools/stubs/full-site-editing-stub-defs.php"

echo
info 'Downloading WooCommerce'
fetch_repo woocommerce/woocommerce
Expand Down

0 comments on commit 236ac36

Please sign in to comment.