wikimedia/mediawiki-core

View on GitHub
includes/libs/mime/MimeAnalyzer.php

Summary

Maintainability
F
1 wk
Test Coverage

File MimeAnalyzer.php has 629 lines of code (exceeds 250 allowed). Consider refactoring.
Open

<?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
Severity: Major
Found in includes/libs/mime/MimeAnalyzer.php - About 1 day to fix

    Function doGuessMimeType has a Cognitive Complexity of 58 (exceeds 5 allowed). Consider refactoring.
    Open

        private function doGuessMimeType( string $file ) {
            // Read a chunk of the file
            AtEase::suppressWarnings();
            $f = fopen( $file, 'rb' );
            AtEase::restoreWarnings();
    Severity: Minor
    Found in includes/libs/mime/MimeAnalyzer.php - About 1 day 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 doGuessMimeType has 173 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

        private function doGuessMimeType( string $file ) {
            // Read a chunk of the file
            AtEase::suppressWarnings();
            $f = fopen( $file, 'rb' );
            AtEase::restoreWarnings();
    Severity: Major
    Found in includes/libs/mime/MimeAnalyzer.php - About 6 hrs to fix

      Function getMediaType has a Cognitive Complexity of 26 (exceeds 5 allowed). Consider refactoring.
      Open

          public function getMediaType( string $path = null, string $mime = null ): string {
              if ( !$mime && !$path ) {
                  return MEDIATYPE_UNKNOWN;
              }
      
      
      Severity: Minor
      Found in includes/libs/mime/MimeAnalyzer.php - About 3 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 parseMimeInfo has a Cognitive Complexity of 24 (exceeds 5 allowed). Consider refactoring.
      Open

          protected function parseMimeInfo( string $rawMimeInfo ): void {
              $rawMimeInfo = str_replace( [ "\r\n", "\n\r", "\n\n", "\r\r", "\r" ], "\n", $rawMimeInfo );
              $rawMimeInfo = str_replace( "\t", " ", $rawMimeInfo );
      
              $lines = explode( "\n", $rawMimeInfo );
      Severity: Minor
      Found in includes/libs/mime/MimeAnalyzer.php - About 3 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

      MimeAnalyzer has 27 functions (exceeds 20 allowed). Consider refactoring.
      Open

      class MimeAnalyzer implements LoggerAwareInterface {
          /** @var string */
          protected $typeFile;
          /** @var string */
          protected $infoFile;
      Severity: Minor
      Found in includes/libs/mime/MimeAnalyzer.php - About 3 hrs to fix

        Method getMediaType has 57 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            public function getMediaType( string $path = null, string $mime = null ): string {
                if ( !$mime && !$path ) {
                    return MEDIATYPE_UNKNOWN;
                }
        
        
        Severity: Major
        Found in includes/libs/mime/MimeAnalyzer.php - About 2 hrs to fix

          Function loadFiles has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.
          Open

              protected function loadFiles(): void {
                  # Allow media handling extensions adding MIME-types and MIME-info
                  if ( $this->initCallback ) {
                      call_user_func( $this->initCallback, $this );
                  }
          Severity: Minor
          Found in includes/libs/mime/MimeAnalyzer.php - About 2 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 detectMimeType has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
          Open

              private function detectMimeType( string $file, $ext = true ): string {
                  /** @todo Make $ext default to false. Or better, remove it. */
                  if ( $ext ) {
                      $this->logger->info( __METHOD__ .
                          ": WARNING: use of the \$ext parameter is deprecated. "
          Severity: Minor
          Found in includes/libs/mime/MimeAnalyzer.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 parseMimeInfo has 41 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

              protected function parseMimeInfo( string $rawMimeInfo ): void {
                  $rawMimeInfo = str_replace( [ "\r\n", "\n\r", "\n\n", "\r\r", "\r" ], "\n", $rawMimeInfo );
                  $rawMimeInfo = str_replace( "\t", " ", $rawMimeInfo );
          
                  $lines = explode( "\n", $rawMimeInfo );
          Severity: Minor
          Found in includes/libs/mime/MimeAnalyzer.php - About 1 hr to fix

            Function improveTypeFromExtension has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
            Open

                public function improveTypeFromExtension( string $mime, string $ext ): ?string {
                    if ( $mime === 'unknown/unknown' ) {
                        if ( $this->isRecognizableExtension( $ext ) ) {
                            $this->logger->info( __METHOD__ . ': refusing to guess mime type for .' .
                                "$ext file, we should have recognized it" );
            Severity: Minor
            Found in includes/libs/mime/MimeAnalyzer.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

            Function parseMimeTypes has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
            Open

                protected function parseMimeTypes( string $rawMimeTypes ): void {
                    $rawMimeTypes = str_replace( [ "\r\n", "\n\r", "\n\n", "\r\r", "\r" ], "\n", $rawMimeTypes );
                    $rawMimeTypes = str_replace( "\t", " ", $rawMimeTypes );
            
                    $lines = explode( "\n", $rawMimeTypes );
            Severity: Minor
            Found in includes/libs/mime/MimeAnalyzer.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 detectMimeType has 39 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

                private function detectMimeType( string $file, $ext = true ): string {
                    /** @todo Make $ext default to false. Or better, remove it. */
                    if ( $ext ) {
                        $this->logger->info( __METHOD__ .
                            ": WARNING: use of the \$ext parameter is deprecated. "
            Severity: Minor
            Found in includes/libs/mime/MimeAnalyzer.php - About 1 hr to fix

              Method loadFiles has 39 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                  protected function loadFiles(): void {
                      # Allow media handling extensions adding MIME-types and MIME-info
                      if ( $this->initCallback ) {
                          call_user_func( $this->initCallback, $this );
                      }
              Severity: Minor
              Found in includes/libs/mime/MimeAnalyzer.php - About 1 hr to fix

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

                    private function findMediaType( string $extMime ) {
                        if ( strpos( $extMime, '.' ) === 0 ) {
                            // If it's an extension, look up the MIME types
                            $m = $this->getMimeTypesFromExtension( substr( $extMime, 1 ) );
                            if ( !$m ) {
                Severity: Minor
                Found in includes/libs/mime/MimeAnalyzer.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 detectZipTypeFromFile has 31 lines of code (exceeds 25 allowed). Consider refactoring.
                Open

                    public function detectZipTypeFromFile( $handle ) {
                        $types = [];
                        $status = ZipDirectoryReader::readHandle(
                            $handle,
                            static function ( $entry ) use ( &$types ) {
                Severity: Minor
                Found in includes/libs/mime/MimeAnalyzer.php - About 1 hr to fix

                  Method improveTypeFromExtension has 30 lines of code (exceeds 25 allowed). Consider refactoring.
                  Open

                      public function improveTypeFromExtension( string $mime, string $ext ): ?string {
                          if ( $mime === 'unknown/unknown' ) {
                              if ( $this->isRecognizableExtension( $ext ) ) {
                                  $this->logger->info( __METHOD__ . ': refusing to guess mime type for .' .
                                      "$ext file, we should have recognized it" );
                  Severity: Minor
                  Found in includes/libs/mime/MimeAnalyzer.php - About 1 hr to fix

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

                        public function detectZipTypeFromFile( $handle ) {
                            $types = [];
                            $status = ZipDirectoryReader::readHandle(
                                $handle,
                                static function ( $entry ) use ( &$types ) {
                    Severity: Minor
                    Found in includes/libs/mime/MimeAnalyzer.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

                    Consider simplifying this complex logical expression.
                    Open

                            if ( ( strpos( $head, '<?php' ) !== false ) ||
                                ( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) ||
                                ( strpos( $head, "<\x00?\x00 " ) !== false ) ||
                                ( strpos( $head, "<\x00?\x00\n" ) !== false ) ||
                                ( strpos( $head, "<\x00?\x00\t" ) !== false ) ||
                    Severity: Major
                    Found in includes/libs/mime/MimeAnalyzer.php - About 40 mins to fix

                      Consider simplifying this complex logical expression.
                      Open

                                      if ( str_starts_with( $data, "webm" ) ) {
                                          // XXX HACK look for a video track, if we don't find it, this is an audio file
                                          // This detection is very naive and doesn't parse the actual fields
                                          // 0x86 byte indicates start of codecname field
                                          // next byte is a variable length integer (vint) for the size of the value following it
                      Severity: Major
                      Found in includes/libs/mime/MimeAnalyzer.php - About 40 mins to fix

                        Avoid too many return statements within this method.
                        Open

                                    return "unknown/unknown";
                        Severity: Major
                        Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                          Avoid too many return statements within this method.
                          Open

                                          return 'application/sla';
                          Severity: Major
                          Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                            Avoid too many return statements within this method.
                            Open

                                        return "image/webp";
                            Severity: Major
                            Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                              Avoid too many return statements within this method.
                              Open

                                              return 'image/jpx';
                              Severity: Major
                              Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                                Avoid too many return statements within this method.
                                Open

                                            return $this->detectMicrosoftBinaryType( $f );
                                Severity: Major
                                Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                                  Avoid too many return statements within this method.
                                  Open

                                                  return $mime;
                                  Severity: Major
                                  Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                                    Avoid too many return statements within this method.
                                    Open

                                                return 'application/sla';
                                    Severity: Major
                                    Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                                      Avoid too many return statements within this method.
                                      Open

                                                  return $mime;
                                      Severity: Major
                                      Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                                        Avoid too many return statements within this method.
                                        Open

                                                            return $type;
                                        Severity: Major
                                        Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                                          Avoid too many return statements within this method.
                                          Open

                                                  return $type;
                                          Severity: Major
                                          Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                                            Avoid too many return statements within this method.
                                            Open

                                                        return $xmlTypes[$xml->getRootElement()] ?? 'application/xml';
                                            Severity: Major
                                            Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                                              Avoid too many return statements within this method.
                                              Open

                                                              return $this->detectZipTypeFromFile( $f );
                                              Severity: Major
                                              Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                                                Avoid too many return statements within this method.
                                                Open

                                                        return $mime;
                                                Severity: Major
                                                Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                                                  Avoid too many return statements within this method.
                                                  Open

                                                                  return MEDIATYPE_AUDIO;
                                                  Severity: Major
                                                  Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                                                    Avoid too many return statements within this method.
                                                    Open

                                                                    return $type;
                                                    Severity: Major
                                                    Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                                                      Avoid too many return statements within this method.
                                                      Open

                                                                          return "audio/webm";
                                                      Severity: Major
                                                      Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                                                        Avoid too many return statements within this method.
                                                        Open

                                                                        return 'image/jp2';
                                                        Severity: Major
                                                        Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                                                          Avoid too many return statements within this method.
                                                          Open

                                                                          return MEDIATYPE_AUDIO;
                                                          Severity: Major
                                                          Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                                                            Avoid too many return statements within this method.
                                                            Open

                                                                            return MEDIATYPE_MULTIMEDIA;
                                                            Severity: Major
                                                            Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                                                              Avoid too many return statements within this method.
                                                              Open

                                                                                      return "video/webm";
                                                              Severity: Major
                                                              Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                                                                Avoid too many return statements within this method.
                                                                Open

                                                                            return 'application/x-php';
                                                                Severity: Major
                                                                Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                                                                  Avoid too many return statements within this method.
                                                                  Open

                                                                                  return $type;
                                                                  Severity: Major
                                                                  Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                                                                    Avoid too many return statements within this method.
                                                                    Open

                                                                                    return MEDIATYPE_AUDIO;
                                                                    Severity: Major
                                                                    Found in includes/libs/mime/MimeAnalyzer.php - About 30 mins to fix

                                                                      There are no issues that match your filters.

                                                                      Category
                                                                      Status