wikimedia/mediawiki-core

View on GitHub
includes/parser/Sanitizer.php

Summary

Maintainability
F
1 wk
Test Coverage

File Sanitizer.php has 1036 lines of code (exceeds 250 allowed). Consider refactoring.
Open

<?php
/**
 * HTML sanitizer for %MediaWiki.
 *
 * Copyright © 2002-2005 Brooke Vibber <bvibber@wikimedia.org> et al
Severity: Major
Found in includes/parser/Sanitizer.php - About 2 days to fix

    Method setupAttributesAllowedInternal has 186 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

        private static function setupAttributesAllowedInternal(): array {
            static $allowed;
    
            if ( $allowed !== null ) {
                return $allowed;
    Severity: Major
    Found in includes/parser/Sanitizer.php - About 7 hrs to fix

      Sanitizer has 49 functions (exceeds 20 allowed). Consider refactoring.
      Open

      class Sanitizer {
          /**
           * Regular expression to match various types of character references in
           * Sanitizer::normalizeCharReferences and Sanitizer::decodeCharReferences.
           * Note that HTML5 allows some named entities to omit the trailing
      Severity: Minor
      Found in includes/parser/Sanitizer.php - About 6 hrs to fix

        Function validateAttributes has a Cognitive Complexity of 36 (exceeds 5 allowed). Consider refactoring.
        Open

            public static function validateAttributes( array $attribs, array $allowed ): array {
                if ( isset( $allowed[0] ) ) {
                    // Calling this function with a sequential array is
                    // deprecated.  For now just convert it.
                    wfDeprecated( __METHOD__ . ' with sequential array', '1.35' );
        Severity: Minor
        Found in includes/parser/Sanitizer.php - About 5 hrs to fix

        Cognitive Complexity

        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

        A method's cognitive complexity is based on a few simple rules:

        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
        • Code is considered more complex for each "break in the linear flow of the code"
        • Code is considered more complex when "flow breaking structures are nested"

        Further reading

        Function internalRemoveHtmlTags has a Cognitive Complexity of 31 (exceeds 5 allowed). Consider refactoring.
        Open

            public static function internalRemoveHtmlTags( string $text, ?callable $processCallback = null,
                $args = [], array $extratags = [], array $removetags = []
            ): string {
                $tagData = self::getRecognizedTagData( $extratags, $removetags );
                $htmlsingle = $tagData['htmlsingle'];
        Severity: Minor
        Found in includes/parser/Sanitizer.php - About 4 hrs to fix

        Cognitive Complexity

        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

        A method's cognitive complexity is based on a few simple rules:

        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
        • Code is considered more complex for each "break in the linear flow of the code"
        • Code is considered more complex when "flow breaking structures are nested"

        Further reading

        Method validateAttributes has 76 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            public static function validateAttributes( array $attribs, array $allowed ): array {
                if ( isset( $allowed[0] ) ) {
                    // Calling this function with a sequential array is
                    // deprecated.  For now just convert it.
                    wfDeprecated( __METHOD__ . ' with sequential array', '1.35' );
        Severity: Major
        Found in includes/parser/Sanitizer.php - About 3 hrs to fix

          Method getRecognizedTagData has 74 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

              public static function getRecognizedTagData( array $extratags = [], array $removetags = [] ): array {
                  static $commonCase, $staticInitialised;
                  $isCommonCase = ( $extratags === [] && $removetags === [] );
                  if ( $staticInitialised === false && $isCommonCase && $commonCase ) {
                      return $commonCase;
          Severity: Major
          Found in includes/parser/Sanitizer.php - About 2 hrs to fix

            Consider simplifying this complex logical expression.
            Open

                        if ( $attribute === 'rel' || $attribute === 'rev'
                            # RDFa
                            || $attribute === 'about' || $attribute === 'property'
                            || $attribute === 'resource' || $attribute === 'datatype'
                            || $attribute === 'typeof'
            Severity: Critical
            Found in includes/parser/Sanitizer.php - About 2 hrs to fix

              Method cleanUrl has 62 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                  public static function cleanUrl( string $url ): string {
                      # Normalize any HTML entities in input. They will be
                      # re-escaped by makeExternalLink().
                      $url = self::decodeCharReferences( $url );
              
              
              Severity: Major
              Found in includes/parser/Sanitizer.php - About 2 hrs to fix

                Method internalRemoveHtmlTags has 40 lines of code (exceeds 25 allowed). Consider refactoring.
                Open

                    public static function internalRemoveHtmlTags( string $text, ?callable $processCallback = null,
                        $args = [], array $extratags = [], array $removetags = []
                    ): string {
                        $tagData = self::getRecognizedTagData( $extratags, $removetags );
                        $htmlsingle = $tagData['htmlsingle'];
                Severity: Minor
                Found in includes/parser/Sanitizer.php - About 1 hr to fix

                  Function removeHTMLcomments has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
                  Open

                      public static function removeHTMLcomments( string $text ): string {
                          while ( ( $start = strpos( $text, '<!--' ) ) !== false ) {
                              $end = strpos( $text, '-->', $start + 4 );
                              if ( $end === false ) {
                                  # Unterminated comment; bail out
                  Severity: Minor
                  Found in includes/parser/Sanitizer.php - About 1 hr to fix

                  Cognitive Complexity

                  Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                  A method's cognitive complexity is based on a few simple rules:

                  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                  • Code is considered more complex for each "break in the linear flow of the code"
                  • Code is considered more complex when "flow breaking structures are nested"

                  Further reading

                  Method removeHTMLcomments has 29 lines of code (exceeds 25 allowed). Consider refactoring.
                  Open

                      public static function removeHTMLcomments( string $text ): string {
                          while ( ( $start = strpos( $text, '<!--' ) ) !== false ) {
                              $end = strpos( $text, '-->', $start + 4 );
                              if ( $end === false ) {
                                  # Unterminated comment; bail out
                  Severity: Minor
                  Found in includes/parser/Sanitizer.php - About 1 hr to fix

                    Method removeSomeTags has 28 lines of code (exceeds 25 allowed). Consider refactoring.
                    Open

                        public static function removeSomeTags(
                            string $text, array $options = []
                        ): string {
                            $extraTags = $options['extraTags'] ?? [];
                            $removeTags = $options['removeTags'] ?? [];
                    Severity: Minor
                    Found in includes/parser/Sanitizer.php - About 1 hr to fix

                      Method safeEncodeAttribute has 26 lines of code (exceeds 25 allowed). Consider refactoring.
                      Open

                          public static function safeEncodeAttribute( string $text ): string {
                              $encValue = self::encodeAttribute( $text );
                      
                              # Templates and links may be expanded in later parsing,
                              # creating invalid or dangerous output. Suppress this.
                      Severity: Minor
                      Found in includes/parser/Sanitizer.php - About 1 hr to fix

                        Function validateTag has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
                        Open

                            private static function validateTag( string $params, string $element ): bool {
                                $params = self::decodeTagAttributes( $params );
                        
                                if ( $element == 'meta' || $element == 'link' ) {
                                    if ( !isset( $params['itemprop'] ) ) {
                        Severity: Minor
                        Found in includes/parser/Sanitizer.php - About 55 mins to fix

                        Cognitive Complexity

                        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                        A method's cognitive complexity is based on a few simple rules:

                        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                        • Code is considered more complex for each "break in the linear flow of the code"
                        • Code is considered more complex when "flow breaking structures are nested"

                        Further reading

                        Function validateCodepoint has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                        Open

                            private static function validateCodepoint( int $codepoint ): bool {
                                # U+000C is valid in HTML5 but not allowed in XML.
                                # U+000D is valid in XML but not allowed in HTML5.
                                # U+007F - U+009F are disallowed in HTML5 (control characters).
                                return $codepoint == 0x09
                        Severity: Minor
                        Found in includes/parser/Sanitizer.php - About 45 mins to fix

                        Cognitive Complexity

                        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                        A method's cognitive complexity is based on a few simple rules:

                        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                        • Code is considered more complex for each "break in the linear flow of the code"
                        • Code is considered more complex when "flow breaking structures are nested"

                        Further reading

                        Method removeHTMLtags has 5 arguments (exceeds 4 allowed). Consider refactoring.
                        Open

                            public static function removeHTMLtags( string $text, ?callable $processCallback = null,
                                $args = [], array $extratags = [], array $removetags = []
                        Severity: Minor
                        Found in includes/parser/Sanitizer.php - About 35 mins to fix

                          Method internalRemoveHtmlTags has 5 arguments (exceeds 4 allowed). Consider refactoring.
                          Open

                              public static function internalRemoveHtmlTags( string $text, ?callable $processCallback = null,
                                  $args = [], array $extratags = [], array $removetags = []
                          Severity: Minor
                          Found in includes/parser/Sanitizer.php - About 35 mins to fix

                            Function getRecognizedTagData has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                            Open

                                public static function getRecognizedTagData( array $extratags = [], array $removetags = [] ): array {
                                    static $commonCase, $staticInitialised;
                                    $isCommonCase = ( $extratags === [] && $removetags === [] );
                                    if ( $staticInitialised === false && $isCommonCase && $commonCase ) {
                                        return $commonCase;
                            Severity: Minor
                            Found in includes/parser/Sanitizer.php - About 35 mins to fix

                            Cognitive Complexity

                            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                            A method's cognitive complexity is based on a few simple rules:

                            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                            • Code is considered more complex for each "break in the linear flow of the code"
                            • Code is considered more complex when "flow breaking structures are nested"

                            Further reading

                            Function cssDecodeCallback has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                            Open

                                private static function cssDecodeCallback( array $matches ): string {
                                    if ( $matches[1] !== '' ) {
                                        // Line continuation
                                        return '';
                                    } elseif ( $matches[2] !== '' ) {
                            Severity: Minor
                            Found in includes/parser/Sanitizer.php - About 35 mins to fix

                            Cognitive Complexity

                            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                            A method's cognitive complexity is based on a few simple rules:

                            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                            • Code is considered more complex for each "break in the linear flow of the code"
                            • Code is considered more complex when "flow breaking structures are nested"

                            Further reading

                            Function hackDocType has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                            Open

                                public static function hackDocType(): string {
                                    $out = "<!DOCTYPE html [\n";
                                    foreach ( HTMLData::$namedEntityTranslations as $entity => $translation ) {
                                        if ( substr( $entity, -1 ) !== ';' ) {
                                            // Some HTML entities omit the trailing semicolon;
                            Severity: Minor
                            Found in includes/parser/Sanitizer.php - About 35 mins to fix

                            Cognitive Complexity

                            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                            A method's cognitive complexity is based on a few simple rules:

                            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                            • Code is considered more complex for each "break in the linear flow of the code"
                            • Code is considered more complex when "flow breaking structures are nested"

                            Further reading

                            Avoid too many return statements within this method.
                            Open

                                    return $matches[0];
                            Severity: Major
                            Found in includes/parser/Sanitizer.php - About 30 mins to fix

                              Function decodeTagAttributes has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                              Open

                                  public static function decodeTagAttributes( string $text ): array {
                                      if ( trim( $text ) == '' ) {
                                          return [];
                                      }
                              
                              
                              Severity: Minor
                              Found in includes/parser/Sanitizer.php - About 25 mins to fix

                              Cognitive Complexity

                              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                              A method's cognitive complexity is based on a few simple rules:

                              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                              • Code is considered more complex for each "break in the linear flow of the code"
                              • Code is considered more complex when "flow breaking structures are nested"

                              Further reading

                              There are no issues that match your filters.

                              Category
                              Status