Skip to content

Commit

Permalink
phan: Add stubs for Photon_OpenCV (#36927)
Browse files Browse the repository at this point in the history
This is a custom PHP extension used on wpcom, which can be downloaded
from https://github.com/Automattic/photon-opencv

Unfortunately its reflection data is broken in a few places (trying to
reflect certain method parameters' types or default values makes PHP
segfault), so we can't hook up any reasonable auto-generation for it.
  • Loading branch information
anomiex authored Apr 18, 2024
1 parent 2f5dc96 commit 7225aae
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .phan/config.base.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* - 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.
* - photon-opencv: Stubs from .phan/stubs/photon-opencv-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 @@ -61,6 +62,9 @@ function make_phan_config( $dir, $options = array() ) {
case 'full-site-editing':
$stubs[] = "$root/.phan/stubs/full-site-editing-stubs.php";
break;
case 'photon-opencv':
$stubs[] = "$root/.phan/stubs/photon-opencv-stubs.php";
break;
case 'woocommerce':
$stubs[] = "$root/vendor/php-stubs/woocommerce-stubs/woocommerce-stubs.php";
break;
Expand Down
61 changes: 61 additions & 0 deletions .phan/stubs/photon-opencv-stubs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Stubs for the photon-opencv extension v0.2.31
*
* These were something of a pain to generate, because the reflection data is
* incorrect for a few methods and causes PHP to segfault.
* - setimageprofile() $profile param crashes on ReflectionParameter->getType()->getName() on PHP 8.1.
* - resizeimage() $fit parameter crashes when ReflectionParameter->getDefaultValue() is called.
* - Presumably the same for scaleimage().
* I had to hack Phan's vendor/phan/phan/tool/make_stubs to get this output.
*/

class Photon_OpenCV {
const CHANNEL_OPACITY = 7;
const COLORSPACE_RGB = 1;
const FILTER_LANCZOS = 13;
const FILTER_CUBIC = 10;
const FILTER_TRIANGLE = 3;
const FILTER_POINT = 1;
const FILTER_BOX = 2;
const IMGTYPE_COLORSEPARATIONMATTE = 9;
const IMGTYPE_GRAYSCALE = 2;
const IMGTYPE_GRAYSCALEMATTE = 3;
const IMGTYPE_PALETTE = 4;
const IMGTYPE_PALETTEMATTE = 5;
const IMGTYPE_TRUECOLOR = 6;
const IMGTYPE_TRUECOLORMATTE = 7;
const ORIENTATION_UNDEFINED = 0;
const ORIENTATION_TOPLEFT = 1;
const ORIENTATION_TOPRIGHT = 2;
const ORIENTATION_BOTTOMRIGHT = 3;
const ORIENTATION_BOTTOMLEFT = 4;
const ORIENTATION_LEFTTOP = 5;
const ORIENTATION_RIGHTTOP = 6;
const ORIENTATION_RIGHTBOTTOM = 7;
const ORIENTATION_LEFTBOTTOM = 8;
public function readimageblob(string &$raw_image_data) {}
public function readimage(string $filepath) {}
public function writeimage(string $output) {}
public function getimageblob() {}
public function getlasterror() {}
public function getimagewidth() {}
public function getimageheight() {}
public function getimagechanneldepth(int $channel) {}
public function getimageformat() {}
public function setimageformat(string $format) {}
public function setcompressionquality(string $format) {}
public function getcompressionquality() {}
public function autoorientimage(int $current_orientation) {}
public function getimageorientation() {}
public function setimageprofile(string $name, $profile) {}
public function getimagetype() {}
public function setimagetype(int $type) {}
public function resizeimage(int $width, int $height, int $filter = -1, bool $fit = false) {}
public function scaleimage(int $width, int $height, bool $fit = false) {}
public function rotateimage(string $background, int $degrees) {}
public function cropimage(int $width, int $height, int $x, int $y) {}
public function borderimage(string $color, int $width, int $height) {}
public function setimageoption(string $format, string $key, string $value) {}
public function blobrequiresreencoding() {}
}
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 @@ -61,7 +61,7 @@
'src/features/launchpad/launchpad-task-definitions.php' => ['PhanRedundantCondition', 'PhanTypeComparisonToArray', 'PhanTypeMismatchArgument', 'PhanTypeMismatchArgumentInternal', 'PhanTypeMismatchReturn', 'PhanTypeMismatchReturnProbablyReal', 'PhanTypeMissingReturn', 'PhanUndeclaredClassInCallable', 'PhanUndeclaredClassMethod', 'PhanUndeclaredFunction', 'PhanUndeclaredTypeParameter'],
'src/features/launchpad/launchpad.php' => ['PhanTypeMismatchArgument', 'PhanTypeMismatchArgumentProbablyReal', 'PhanTypeMismatchReturn', 'PhanTypeMismatchReturnProbablyReal', 'PhanUndeclaredClassMethod', 'PhanUndeclaredFunction', 'PhanUndeclaredTypeParameter', 'PhanUndeclaredTypeReturnType'],
'src/features/marketplace-products-updater/class-marketplace-products-updater.php' => ['PhanTypeMismatchDimFetch', 'PhanTypeMismatchReturn', 'PhanUndeclaredClassMethod', 'PhanUndeclaredClassReference', 'PhanUnextractableAnnotationSuffix'],
'src/features/media/heif-support.php' => ['PhanPluginSimplifyExpressionBool', 'PhanUndeclaredClassMethod', 'PhanUndeclaredFunction'],
'src/features/media/heif-support.php' => ['PhanPluginSimplifyExpressionBool', 'PhanUndeclaredFunction'],
'src/features/verbum-comments/assets/class-wpcom-rest-api-v2-verbum-auth.php' => ['PhanUndeclaredFunction'],
'src/features/verbum-comments/class-verbum-comments.php' => ['PhanImpossibleTypeComparison', 'PhanNoopNew', 'PhanParamTooMany', 'PhanTypeMismatchArgumentProbablyReal', 'PhanTypeMismatchReturn', 'PhanUndeclaredClassMethod', 'PhanUndeclaredConstant', 'PhanUndeclaredFunction', 'PhanUndeclaredFunctionInCallable', 'PhanUndeclaredTypeReturnType'],
'src/features/wpcom-command-palette/wpcom-command-palette.php' => ['PhanUndeclaredClassMethod', 'PhanUndeclaredFunction'],
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/jetpack-mu-wpcom/.phan/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
return make_phan_config(
dirname( __DIR__ ),
array(
'+stubs' => array( 'full-site-editing' ),
'+stubs' => array( 'full-site-editing', 'photon-opencv' ),
'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: changed
Comment: Update Phan config to use photon-opencv stubs. No change to functionality.


0 comments on commit 7225aae

Please sign in to comment.