From dc82db422bcdff85d0ea1f9e21b7a4202cf9d87e Mon Sep 17 00:00:00 2001 From: Thiemo Kreuz Date: Tue, 1 Feb 2022 15:49:06 +0100 Subject: [PATCH] Use more specific `instanceof stdClass` instead of is_object() PHP is a little weird in so far that what you get from e.g. `(object)[]` or json_decode() are not objects but stdClass instances. You can think of stdClass as a subclass of object, i.e. it's more specific. Using is_object() means that stuff like ArrayIterator will be accepted, which is not correct in this context here. Change-Id: I0bffc54508ac7a27bbb59c3aabb9695158eb96b3 --- includes/TemplateDataHtmlFormatter.php | 1 + includes/TemplateDataValidator.php | 13 +++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/includes/TemplateDataHtmlFormatter.php b/includes/TemplateDataHtmlFormatter.php index e359726f..0024a167 100644 --- a/includes/TemplateDataHtmlFormatter.php +++ b/includes/TemplateDataHtmlFormatter.php @@ -28,6 +28,7 @@ class TemplateDataHtmlFormatter { // The following icon names are used here: // * template-format-block // * template-format-inline + // @phan-suppress-next-line PhanTypeSuspiciousStringExpression $icon = 'template-format-' . $data->format; $formatMsg = $data->format; } else { diff --git a/includes/TemplateDataValidator.php b/includes/TemplateDataValidator.php index fd740e5c..f1b83a28 100644 --- a/includes/TemplateDataValidator.php +++ b/includes/TemplateDataValidator.php @@ -70,7 +70,7 @@ class TemplateDataValidator { return Status::newFatal( 'templatedata-invalid-parse' ); } - if ( !is_object( $data ) ) { + if ( !( $data instanceof stdClass ) ) { return Status::newFatal( 'templatedata-invalid-type', 'templatedata', 'object' ); } @@ -93,6 +93,7 @@ class TemplateDataValidator { // Root.format if ( isset( $data->format ) ) { + // @phan-suppress-next-line PhanTypeMismatchDimFetchNullable $f = self::PREDEFINED_FORMATS[$data->format] ?? $data->format; if ( !is_string( $f ) || !preg_match( '/^\n?\{\{ *_+\n? *\|\n? *_+ *= *_+\n? *\}\}\n?$/', $f ) @@ -108,7 +109,7 @@ class TemplateDataValidator { return Status::newFatal( 'templatedata-invalid-missing', 'params', 'object' ); } - if ( !is_object( $data->params ) ) { + if ( !( $data->params instanceof stdClass ) ) { return Status::newFatal( 'templatedata-invalid-type', 'params', 'object' ); } @@ -118,7 +119,7 @@ class TemplateDataValidator { $unnormalizedParams = unserialize( serialize( $data->params ) ); foreach ( $data->params as $paramName => $param ) { - if ( !is_object( $param ) ) { + if ( !( $param instanceof stdClass ) ) { return Status::newFatal( 'templatedata-invalid-type', "params.{$paramName}", 'object' ); } @@ -329,7 +330,7 @@ class TemplateDataValidator { } foreach ( $data->sets as $setNr => $setObj ) { - if ( !is_object( $setObj ) ) { + if ( !( $setObj instanceof stdClass ) ) { return Status::newFatal( 'templatedata-invalid-value', "sets.{$setNr}" ); } @@ -370,7 +371,7 @@ class TemplateDataValidator { // Root.maps if ( isset( $data->maps ) ) { - if ( !is_object( $data->maps ) ) { + if ( !( $data->maps instanceof stdClass ) ) { return Status::newFatal( 'templatedata-invalid-type', 'maps', 'object' ); } } else { @@ -378,7 +379,7 @@ class TemplateDataValidator { } foreach ( $data->maps as $consumerId => $map ) { - if ( !is_object( $map ) ) { + if ( !( $map instanceof stdClass ) ) { return Status::newFatal( 'templatedata-invalid-type', "maps.$consumerId", 'object' ); }