From d5f0ea9fe69db5430cc0ab76f6f67320cf152f73 Mon Sep 17 00:00:00 2001 From: Damian Taggart <4309872+attackant@users.noreply.github.com> Date: Tue, 31 Oct 2023 10:38:10 -0600 Subject: [PATCH] "Add PHP Code Sniffer ignores for snake case naming checks The project has been updated to add comments to ignore the 'not using snake case' warnings from PHP Code Sniffer. This was necessary as the property names used in the Node class provided by the core PHP library, do not follow snake case convention. These warnings were leading to unnecessary noise in the linting reports and were misleading as it could lead developers to think that there's something wrong with the project's own codebase. As this convention is not something developer can change, it is now simply ignored by the linter." --- includes/REST/apple-news-get-settings.php | 2 +- includes/apple-exporter/class-component-factory.php | 2 ++ includes/apple-exporter/class-exporter-content.php | 2 +- includes/apple-exporter/class-markdown.php | 3 +++ includes/apple-exporter/components/class-body.php | 12 +++++++----- .../components/class-embed-generic.php | 3 ++- .../apple-exporter/components/class-facebook.php | 2 ++ includes/apple-exporter/components/class-gallery.php | 6 +++--- includes/apple-exporter/components/class-heading.php | 4 ++-- includes/apple-exporter/components/class-image.php | 2 ++ .../apple-exporter/components/class-instagram.php | 4 ++-- .../apple-exporter/components/class-link-button.php | 4 ++-- includes/apple-exporter/components/class-podcast.php | 6 +++--- includes/apple-exporter/components/class-quote.php | 2 +- includes/apple-exporter/components/class-table.php | 4 ++-- includes/apple-exporter/components/class-tiktok.php | 2 ++ includes/apple-push-api/class-mime-builder.php | 10 +++++----- tests/admin/test-class-admin-apple-themes.php | 2 +- tests/mocks/class-bc-cms-api.php | 2 +- 19 files changed, 44 insertions(+), 30 deletions(-) diff --git a/includes/REST/apple-news-get-settings.php b/includes/REST/apple-news-get-settings.php index 691b56776..bd9c2e36c 100644 --- a/includes/REST/apple-news-get-settings.php +++ b/includes/REST/apple-news-get-settings.php @@ -15,7 +15,7 @@ * @param array $data data from query args. * @return array updated response. */ -function get_settings_response( $data ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable +function get_settings_response( $data ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found // Ensure Apple News is first initialized. \Apple_News::has_uninitialized_error(); diff --git a/includes/apple-exporter/class-component-factory.php b/includes/apple-exporter/class-component-factory.php index 62ba484df..ec07e2b64 100644 --- a/includes/apple-exporter/class-component-factory.php +++ b/includes/apple-exporter/class-component-factory.php @@ -193,6 +193,7 @@ public static function get_component( $shortname, $html ) { * @return array An array of components contained in the node. */ public static function get_components_from_node( $node ) { + /* phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */ $result = []; foreach ( self::$components as $shortname => $class ) { @@ -241,5 +242,6 @@ public static function get_components_from_node( $node ) { } return $result; + /* phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */ } } diff --git a/includes/apple-exporter/class-exporter-content.php b/includes/apple-exporter/class-exporter-content.php index 0ec0d04c7..988ce69a0 100644 --- a/includes/apple-exporter/class-exporter-content.php +++ b/includes/apple-exporter/class-exporter-content.php @@ -292,6 +292,6 @@ public function nodes() { // Find the first-level nodes of the body tag. $body = $dom->getElementsByTagName( 'body' )->item( 0 ); - return $body ? $body->childNodes : new \DOMNodeList(); + return $body ? $body->childNodes : new \DOMNodeList(); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase } } diff --git a/includes/apple-exporter/class-markdown.php b/includes/apple-exporter/class-markdown.php index 71ed80c8b..b91e54175 100644 --- a/includes/apple-exporter/class-markdown.php +++ b/includes/apple-exporter/class-markdown.php @@ -9,6 +9,8 @@ * @since 0.2.0 */ +/* phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */ + namespace Apple_Exporter; use DOMElement; @@ -306,3 +308,4 @@ private function parse_node_unordered_list( $node ) { return $text . "\n\n"; } } +/* phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */ diff --git a/includes/apple-exporter/components/class-body.php b/includes/apple-exporter/components/class-body.php index 6d62e93a7..5e00b9a04 100644 --- a/includes/apple-exporter/components/class-body.php +++ b/includes/apple-exporter/components/class-body.php @@ -40,6 +40,7 @@ class Body extends Component { * @return array|null The node on success, or null on no match. */ public static function node_matches( $node ) { + /* phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */ // We are only interested in p, pre, ul and ol. if ( ! in_array( $node->nodeName, [ 'p', 'pre', 'ul', 'ol' ], true ) ) { return null; @@ -88,6 +89,7 @@ public static function node_matches( $node ) { $open, $close ); + /* phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */ } /** @@ -441,7 +443,7 @@ public function set_default_style() { /** * Determine whether to apply a dropcap style for the component. - * + * * @param \Apple_Exporter\Theme $theme Object that stores theme level dropcap configuration. * @param string $html The HTML to check for dropcap conditions. Should be the first paragraph of the post content. * @@ -471,17 +473,17 @@ private function dropcap_determination( $theme, $html ) { $use_dropcap = false; } - /** + /** * Allows for filtering of the dropcap content before return. - * + * * @since 2.4.0 - * + * * @param bool $use_dropcap Whether to apply a dropcap to this paragraph or not. * @param string $html The post content to filter. * @param \Apple_Exporter\Theme $theme The theme whose dropcap options are used. * @param string $post_id The id of the post whose content we're parsing. */ - return apply_filters( 'apple_news_dropcap', $use_dropcap, $html, $theme, $this->workspace->content_id ); + return apply_filters( 'apple_news_dropcap', $use_dropcap, $html, $theme, $this->workspace->content_id ); } /** diff --git a/includes/apple-exporter/components/class-embed-generic.php b/includes/apple-exporter/components/class-embed-generic.php index 341ac6ecd..f27249cdd 100644 --- a/includes/apple-exporter/components/class-embed-generic.php +++ b/includes/apple-exporter/components/class-embed-generic.php @@ -27,7 +27,7 @@ class Embed_Generic extends Component { * @return \DOMElement|null The node on success, or null on no match. */ public static function node_matches( $node ) { - + /* phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */ // If we aren't on an element node, bail out. if ( 1 !== $node->nodeType ) { return null; @@ -51,6 +51,7 @@ public static function node_matches( $node ) { && 'iframe' === $node->childNodes->item( 0 )->nodeName ) { return $node; + /* phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */ } // Anything else isn't supported out of the box. diff --git a/includes/apple-exporter/components/class-facebook.php b/includes/apple-exporter/components/class-facebook.php index da0809a7f..4d1f57c70 100644 --- a/includes/apple-exporter/components/class-facebook.php +++ b/includes/apple-exporter/components/class-facebook.php @@ -62,6 +62,7 @@ public function register_specs() { */ public static function node_matches( $node ) { + /* phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */ // Handling for a Gutenberg Facebook embed. if ( 'figure' === $node->nodeName @@ -105,6 +106,7 @@ public static function node_matches( $node ) { ) { return $node; } + /* phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */ } // facebook not found. diff --git a/includes/apple-exporter/components/class-gallery.php b/includes/apple-exporter/components/class-gallery.php index d8c15e295..18e1e9066 100644 --- a/includes/apple-exporter/components/class-gallery.php +++ b/includes/apple-exporter/components/class-gallery.php @@ -109,17 +109,17 @@ protected function build( $html ) { } // Determine if we have items. - if ( empty( $container->childNodes ) ) { + if ( empty( $container->childNodes ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase return; } // Loop through items and construct slides. $theme = \Apple_Exporter\Theme::get_used(); $items = []; - foreach ( $container->childNodes as $item ) { + foreach ( $container->childNodes as $item ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase // Convert item into HTML for regex matching. - $item_html = $item->ownerDocument->saveXML( $item ); + $item_html = $item->ownerDocument->saveXML( $item ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase // Try to get URL. if ( ! preg_match( '/src="([^"]+)"/', $item_html, $matches ) ) { diff --git a/includes/apple-exporter/components/class-heading.php b/includes/apple-exporter/components/class-heading.php index 61ec1e3e4..5af2476b7 100644 --- a/includes/apple-exporter/components/class-heading.php +++ b/includes/apple-exporter/components/class-heading.php @@ -37,11 +37,11 @@ public static function node_matches( $node ) { self::$levels[ count( self::$levels ) - 1 ] ); - if ( ! preg_match( $regex, $node->nodeName ) ) { + if ( ! preg_match( $regex, $node->nodeName ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase return null; } - $html = $node->ownerDocument->saveXML( $node ); + $html = $node->ownerDocument->saveXML( $node ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase if ( preg_match( '##si', $html ) ) { return self::split_image( $html ); } diff --git a/includes/apple-exporter/components/class-image.php b/includes/apple-exporter/components/class-image.php index ff52a5e17..558173057 100644 --- a/includes/apple-exporter/components/class-image.php +++ b/includes/apple-exporter/components/class-image.php @@ -23,6 +23,7 @@ class Image extends Component { * @return \DOMElement|null The node on success, or null on no match. */ public static function node_matches( $node ) { + /* phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */ $has_image_child = false; // If this is a figure and it has children, see if we can find an image. @@ -48,6 +49,7 @@ public static function node_matches( $node ) { } return null; + /* phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */ } /** diff --git a/includes/apple-exporter/components/class-instagram.php b/includes/apple-exporter/components/class-instagram.php index 8198c8d0e..42056657c 100644 --- a/includes/apple-exporter/components/class-instagram.php +++ b/includes/apple-exporter/components/class-instagram.php @@ -29,14 +29,14 @@ public static function node_matches( $node ) { // Handling for a Gutenberg Instagram embed. if ( - 'figure' === $node->nodeName + 'figure' === $node->nodeName // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase && self::node_has_class( $node, 'wp-block-embed-instagram' ) ) { return $node; } // Handle Instagram oEmbed URLs. - if ( false !== self::get_instagram_url( $node->nodeValue ) ) { + if ( false !== self::get_instagram_url( $node->nodeValue ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase return $node; } diff --git a/includes/apple-exporter/components/class-link-button.php b/includes/apple-exporter/components/class-link-button.php index 939f2e453..556600f0c 100644 --- a/includes/apple-exporter/components/class-link-button.php +++ b/includes/apple-exporter/components/class-link-button.php @@ -23,10 +23,10 @@ class Link_Button extends Component { * @return \DOMElement|null The node on success, or null on no match. */ public static function node_matches( $node ) { - return 'a' === $node->nodeName + return 'a' === $node->nodeName // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase && self::node_has_class( $node, 'wp-block-button__link' ) && ! empty( $node->getAttribute( 'href' ) ) - && ! empty( $node->nodeValue ) + && ! empty( $node->nodeValue ) // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase ? $node : null; } diff --git a/includes/apple-exporter/components/class-podcast.php b/includes/apple-exporter/components/class-podcast.php index 4acdd57b8..8d2673a56 100644 --- a/includes/apple-exporter/components/class-podcast.php +++ b/includes/apple-exporter/components/class-podcast.php @@ -23,7 +23,7 @@ class Podcast extends Component { * Look for node matches for this component. * * @param \DOMElement $node The node to examine for matches. - * + * * @return \DOMElement|null The node on success, or null on no match. */ public static function node_matches( $node ) { @@ -100,9 +100,9 @@ private static function get_podcast_url( $node ) { // Remove iframe specific `embed.` prefix on podcast url. $url = str_replace( 'embed.podcasts.apple.com', 'podcasts.apple.com', $url ); - + // Parse url into component parts. - $url_comps = parse_url( $url ); + $url_comps = wp_parse_url( $url ); // Reassemble url without query params. $url = sprintf( diff --git a/includes/apple-exporter/components/class-quote.php b/includes/apple-exporter/components/class-quote.php index 28c187951..e79e5a90a 100644 --- a/includes/apple-exporter/components/class-quote.php +++ b/includes/apple-exporter/components/class-quote.php @@ -26,7 +26,7 @@ class Quote extends Component { * @return \DOMElement|null The node on success, or null on no match. */ public static function node_matches( $node ) { - return ( 'blockquote' === $node->nodeName || ( isset( $node->firstChild->tagName ) && 'blockquote' === $node->firstChild->tagName ) ) ? $node : null; + return ( 'blockquote' === $node->nodeName || ( isset( $node->firstChild->tagName ) && 'blockquote' === $node->firstChild->tagName ) ) ? $node : null; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase } /** diff --git a/includes/apple-exporter/components/class-table.php b/includes/apple-exporter/components/class-table.php index 784485e82..71d5290ee 100644 --- a/includes/apple-exporter/components/class-table.php +++ b/includes/apple-exporter/components/class-table.php @@ -42,9 +42,9 @@ public static function node_matches( $node ) { ( self::node_has_class( $node, 'wp-block-table' ) && $node->hasChildNodes() && - 'table' === $node->firstChild->nodeName + 'table' === $node->firstChild->nodeName // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase ) || - 'table' === $node->nodeName ) { + 'table' === $node->nodeName ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase return $node; } diff --git a/includes/apple-exporter/components/class-tiktok.php b/includes/apple-exporter/components/class-tiktok.php index 45b50ba09..d9c4ca44f 100644 --- a/includes/apple-exporter/components/class-tiktok.php +++ b/includes/apple-exporter/components/class-tiktok.php @@ -25,6 +25,7 @@ class TikTok extends Component { * @return \DOMElement|null The node on success, or null on no match. */ public static function node_matches( $node ) { + /* phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */ // Handling for a Gutenberg TikTok embed. if ( @@ -45,6 +46,7 @@ public static function node_matches( $node ) { && self::node_has_class( $node, 'tiktok-embed' ) ) { return $node; + /* phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */ } return null; diff --git a/includes/apple-push-api/class-mime-builder.php b/includes/apple-push-api/class-mime-builder.php index 505ac9cf7..5e93fe448 100644 --- a/includes/apple-push-api/class-mime-builder.php +++ b/includes/apple-push-api/class-mime-builder.php @@ -210,8 +210,8 @@ private function build_attachment( $name, $filename, $content, $mime_type, $size throw new Request_Exception( sprintf( // translators: token is an attachment filename. - __( 'The attachment %s could not be included in the request because it was empty.', 'apple-news' ), - esc_html( $filename ) + esc_html__( 'The attachment %s could not be included in the request because it was empty.', 'apple-news' ), + $filename // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped ) ); } @@ -221,9 +221,9 @@ private function build_attachment( $name, $filename, $content, $mime_type, $size throw new Request_Exception( sprintf( // translators: first token is the filename, second is the file size. - __( 'The attachment %1$s could not be included in the request because its size was %2$s.', 'apple-news' ), - esc_html( $filename ), - esc_html( $size ) + esc_html__( 'The attachment %1$s could not be included in the request because its size was %2$s.', 'apple-news' ), + $filename, // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped + $size // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped ) ); } diff --git a/tests/admin/test-class-admin-apple-themes.php b/tests/admin/test-class-admin-apple-themes.php index 3e580c411..441a9edd1 100644 --- a/tests/admin/test-class-admin-apple-themes.php +++ b/tests/admin/test-class-admin-apple-themes.php @@ -13,7 +13,7 @@ use Apple_Exporter\Theme; /** - * + * A class to test the Admin_Apple_Themes class. */ class Admin_Apple_Themes_Test extends Apple_News_Testcase { diff --git a/tests/mocks/class-bc-cms-api.php b/tests/mocks/class-bc-cms-api.php index a8062bf2b..e8f4f4d6b 100644 --- a/tests/mocks/class-bc-cms-api.php +++ b/tests/mocks/class-bc-cms-api.php @@ -22,7 +22,7 @@ class BC_CMS_API { * * @return array Array of the video's images retrieved. */ - public function video_get_images( $video_id ) { + public function video_get_images( $video_id ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found return [ 'poster' => [ 'src' => 'https://cf-images.us-east-1.prod.boltdns.net/v1/jit/1234567890/abcd1234-ef56-ab78-cd90-efabcd123456/main/1280x720/1s234ms/match/image.jpg',