wikimedia/mediawiki-core

View on GitHub
includes/api/ApiUpload.php

Summary

Maintainability
F
1 wk
Test Coverage

File ApiUpload.php has 871 lines of code (exceeds 250 allowed). Consider refactoring.
Open

<?php
/**
 * Copyright © 2008 - 2010 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
Severity: Major
Found in includes/api/ApiUpload.php - About 2 days to fix

    Function selectUploadModule has a Cognitive Complexity of 42 (exceeds 5 allowed). Consider refactoring.
    Open

        protected function selectUploadModule() {
            // chunk or one and only one of the following parameters is needed
            if ( !$this->mParams['chunk'] ) {
                $this->requireOnlyOneParameter( $this->mParams,
                    'filekey', 'file', 'url' );
    Severity: Minor
    Found in includes/api/ApiUpload.php - About 6 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 getChunkResult has 152 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

        private function getChunkResult( $warnings ) {
            $result = [];
    
            if ( $warnings && count( $warnings ) > 0 ) {
                $result['warnings'] = $warnings;
    Severity: Major
    Found in includes/api/ApiUpload.php - About 6 hrs to fix

      Method performUpload has 103 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          protected function performUpload( $warnings ) {
              // Use comment as initial page text by default
              $this->mParams['text'] ??= $this->mParams['comment'];
      
              /** @var LocalFile $file */
      Severity: Major
      Found in includes/api/ApiUpload.php - About 4 hrs to fix

        Method selectUploadModule has 95 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            protected function selectUploadModule() {
                // chunk or one and only one of the following parameters is needed
                if ( !$this->mParams['chunk'] ) {
                    $this->requireOnlyOneParameter( $this->mParams,
                        'filekey', 'file', 'url' );
        Severity: Major
        Found in includes/api/ApiUpload.php - About 3 hrs to fix

          Function getChunkResult has a Cognitive Complexity of 24 (exceeds 5 allowed). Consider refactoring.
          Open

              private function getChunkResult( $warnings ) {
                  $result = [];
          
                  if ( $warnings && count( $warnings ) > 0 ) {
                      $result['warnings'] = $warnings;
          Severity: Minor
          Found in includes/api/ApiUpload.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

          ApiUpload has 26 functions (exceeds 20 allowed). Consider refactoring.
          Open

          class ApiUpload extends ApiBase {
          
              use ApiWatchlistTrait;
          
              /** @var UploadBase|UploadFromChunks */
          Severity: Minor
          Found in includes/api/ApiUpload.php - About 3 hrs to fix

            Function performUpload has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring.
            Open

                protected function performUpload( $warnings ) {
                    // Use comment as initial page text by default
                    $this->mParams['text'] ??= $this->mParams['comment'];
            
                    /** @var LocalFile $file */
            Severity: Minor
            Found in includes/api/ApiUpload.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

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

                protected function checkVerification( array $verification ) {
                    switch ( $verification['status'] ) {
                        // Recoverable errors
                        case UploadBase::MIN_LENGTH_PARTNAME:
                            $this->dieRecoverableError( [ 'filename-tooshort' ], 'filename' );
            Severity: Major
            Found in includes/api/ApiUpload.php - About 2 hrs to fix

              Method execute has 56 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                  public function execute() {
                      // Check whether upload is enabled
                      if ( !UploadBase::isEnabled() ) {
                          $this->dieWithError( 'uploaddisabled' );
                      }
              Severity: Major
              Found in includes/api/ApiUpload.php - About 2 hrs to fix

                Function execute has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
                Open

                    public function execute() {
                        // Check whether upload is enabled
                        if ( !UploadBase::isEnabled() ) {
                            $this->dieWithError( 'uploaddisabled' );
                        }
                Severity: Minor
                Found in includes/api/ApiUpload.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 verifyUpload has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
                Open

                    protected function verifyUpload() {
                        if ( $this->mParams['chunk'] ) {
                            $maxSize = UploadBase::getMaxUploadSize();
                            if ( $this->mParams['filesize'] > $maxSize ) {
                                $this->dieWithError( 'file-too-large' );
                Severity: Minor
                Found in includes/api/ApiUpload.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

                Method getAllowedParams has 51 lines of code (exceeds 25 allowed). Consider refactoring.
                Open

                    public function getAllowedParams() {
                        $params = [
                            'filename' => [
                                ParamValidator::PARAM_TYPE => 'string',
                            ],
                Severity: Major
                Found in includes/api/ApiUpload.php - About 2 hrs to fix

                  Method performStash has 50 lines of code (exceeds 25 allowed). Consider refactoring.
                  Open

                      private function performStash( $failureMode, &$data = null ) {
                          $isPartial = (bool)$this->mParams['chunk'];
                          try {
                              $status = $this->mUpload->tryStashFile( $this->getUser(), $isPartial );
                  
                  
                  Severity: Minor
                  Found in includes/api/ApiUpload.php - About 2 hrs to fix

                    Function transformWarnings has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
                    Open

                        protected function transformWarnings( $warnings ) {
                            if ( $warnings ) {
                                // Add indices
                                ApiResult::setIndexedTagName( $warnings, 'warning' );
                    
                    
                    Severity: Minor
                    Found in includes/api/ApiUpload.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 handleStashException has 38 lines of code (exceeds 25 allowed). Consider refactoring.
                    Open

                        protected function handleStashException( $e ) {
                            $this->log->info( "Upload stashing of {filename} failed for {user} because {error}",
                                [
                                    'user' => $this->getUser()->getName(),
                                    'error' => get_class( $e ),
                    Severity: Minor
                    Found in includes/api/ApiUpload.php - About 1 hr to fix

                      Method transformWarnings has 36 lines of code (exceeds 25 allowed). Consider refactoring.
                      Open

                          protected function transformWarnings( $warnings ) {
                              if ( $warnings ) {
                                  // Add indices
                                  ApiResult::setIndexedTagName( $warnings, 'warning' );
                      
                      
                      Severity: Minor
                      Found in includes/api/ApiUpload.php - About 1 hr to fix

                        Method verifyUpload has 35 lines of code (exceeds 25 allowed). Consider refactoring.
                        Open

                            protected function verifyUpload() {
                                if ( $this->mParams['chunk'] ) {
                                    $maxSize = UploadBase::getMaxUploadSize();
                                    if ( $this->mParams['filesize'] > $maxSize ) {
                                        $this->dieWithError( 'file-too-large' );
                        Severity: Minor
                        Found in includes/api/ApiUpload.php - About 1 hr to fix

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

                              protected function handleStashException( $e ) {
                                  $this->log->info( "Upload stashing of {filename} failed for {user} because {error}",
                                      [
                                          'user' => $this->getUser()->getName(),
                                          'error' => get_class( $e ),
                          Severity: Minor
                          Found in includes/api/ApiUpload.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 __construct has 5 arguments (exceeds 4 allowed). Consider refactoring.
                          Open

                                  ApiMain $mainModule,
                                  $moduleName,
                                  JobQueueGroup $jobQueueGroup,
                                  WatchlistManager $watchlistManager,
                                  UserOptionsLookup $userOptionsLookup
                          Severity: Minor
                          Found in includes/api/ApiUpload.php - About 35 mins to fix

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

                                private function performStash( $failureMode, &$data = null ) {
                                    $isPartial = (bool)$this->mParams['chunk'];
                                    try {
                                        $status = $this->mUpload->tryStashFile( $this->getUser(), $isPartial );
                            
                            
                            Severity: Minor
                            Found in includes/api/ApiUpload.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

                            Similar blocks of code found in 2 locations. Consider refactoring.
                            Open

                                        if ( $this->mParams['filekey'] ) {
                                            $job = new PublishStashedFileJob(
                                                [
                                                    'filename' => $this->mParams['filename'],
                                                    'filekey' => $this->mParams['filekey'],
                            Severity: Major
                            Found in includes/api/ApiUpload.php and 1 other location - About 1 hr to fix
                            includes/api/ApiUpload.php on lines 1086..1100

                            Duplicated Code

                            Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                            Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                            When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                            Tuning

                            This issue has a mass of 108.

                            We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                            The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                            If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                            See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                            Refactorings

                            Further Reading

                            Similar blocks of code found in 2 locations. Consider refactoring.
                            Open

                                        } elseif ( $this->mParams['url'] ) {
                                            $job = new UploadFromUrlJob(
                                                [
                                                    'filename' => $this->mParams['filename'],
                                                    'url' => $this->mParams['url'],
                            Severity: Major
                            Found in includes/api/ApiUpload.php and 1 other location - About 1 hr to fix
                            includes/api/ApiUpload.php on lines 1072..1106

                            Duplicated Code

                            Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                            Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                            When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                            Tuning

                            This issue has a mass of 108.

                            We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                            The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                            If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                            See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                            Refactorings

                            Further Reading

                            There are no issues that match your filters.

                            Category
                            Status