Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (7 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/testcase.php

    r41945 r42343  
    1515class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
    1616
    17     protected static $forced_tickets = array();
    18     protected $expected_deprecated = array();
    19     protected $caught_deprecated = array();
     17    protected static $forced_tickets   = array();
     18    protected $expected_deprecated     = array();
     19    protected $caught_deprecated       = array();
    2020    protected $expected_doing_it_wrong = array();
    21     protected $caught_doing_it_wrong = array();
     21    protected $caught_doing_it_wrong   = array();
    2222
    2323    protected static $hooks_saved = array();
     
    2626    function __isset( $name ) {
    2727        return 'factory' === $name;
    28     }
     28    }
    2929
    3030    function __get( $name ) {
    3131        if ( 'factory' === $name ) {
    3232            return self::factory();
    33         }
    34     }
     33        }
     34    }
    3535
    3636    /**
     
    5656        // [0] WP_UnitTestCase::get_called_class()
    5757        // [1] WP_UnitTestCase::setUpBeforeClass()
    58         if ( 'call_user_func' ===  $backtrace[2]['function'] ) {
     58        if ( 'call_user_func' === $backtrace[2]['function'] ) {
    5959            return $backtrace[2]['args'][0][0];
    6060        }
     
    6666
    6767        $wpdb->suppress_errors = false;
    68         $wpdb->show_errors = true;
     68        $wpdb->show_errors     = true;
    6969        $wpdb->db_connect();
    70         ini_set('display_errors', 1 );
     70        ini_set( 'display_errors', 1 );
    7171
    7272        parent::setUpBeforeClass();
     
    101101
    102102    function setUp() {
    103         set_time_limit(0);
     103        set_time_limit( 0 );
    104104
    105105        if ( ! self::$ignore_files ) {
     
    160160        }
    161161        $wp_query = new WP_Query();
    162         $wp = new WP();
     162        $wp       = new WP();
    163163
    164164        // Reset globals related to the post loop and `setup_postdata()`.
     
    177177
    178178    function clean_up_global_scope() {
    179         $_GET = array();
     179        $_GET  = array();
    180180        $_POST = array();
    181181        self::flush_cache();
     
    185185     * Allow tests to be skipped on some automated runs
    186186     *
    187      * For test runs on Travis for something other than trunk/master 
     187     * For test runs on Travis for something other than trunk/master
    188188     * we want to skip tests that only need to run for master.
    189189     */
     
    321321    static function flush_cache() {
    322322        global $wp_object_cache;
    323         $wp_object_cache->group_ops = array();
    324         $wp_object_cache->stats = array();
     323        $wp_object_cache->group_ops      = array();
     324        $wp_object_cache->stats          = array();
    325325        $wp_object_cache->memcache_debug = array();
    326         $wp_object_cache->cache = array();
     326        $wp_object_cache->cache          = array();
    327327        if ( method_exists( $wp_object_cache, '__remoteset' ) ) {
    328328            $wp_object_cache->__remoteset();
     
    352352
    353353    function _create_temporary_tables( $query ) {
    354         if ( 'CREATE TABLE' === substr( trim( $query ), 0, 12 ) )
     354        if ( 'CREATE TABLE' === substr( trim( $query ), 0, 12 ) ) {
    355355            return substr_replace( trim( $query ), 'CREATE TEMPORARY TABLE', 0, 12 );
     356        }
    356357        return $query;
    357358    }
    358359
    359360    function _drop_temporary_tables( $query ) {
    360         if ( 'DROP TABLE' === substr( trim( $query ), 0, 10 ) )
     361        if ( 'DROP TABLE' === substr( trim( $query ), 0, 10 ) ) {
    361362            return substr_replace( trim( $query ), 'DROP TEMPORARY TABLE', 0, 10 );
     363        }
    362364        return $query;
    363365    }
     
    378380        $annotations = $this->getAnnotations();
    379381        foreach ( array( 'class', 'method' ) as $depth ) {
    380             if ( ! empty( $annotations[ $depth ]['expectedDeprecated'] ) )
     382            if ( ! empty( $annotations[ $depth ]['expectedDeprecated'] ) ) {
    381383                $this->expected_deprecated = array_merge( $this->expected_deprecated, $annotations[ $depth ]['expectedDeprecated'] );
    382             if ( ! empty( $annotations[ $depth ]['expectedIncorrectUsage'] ) )
     384            }
     385            if ( ! empty( $annotations[ $depth ]['expectedIncorrectUsage'] ) ) {
    383386                $this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, $annotations[ $depth ]['expectedIncorrectUsage'] );
     387            }
    384388        }
    385389        add_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) );
     
    389393        add_action( 'deprecated_function_trigger_error', '__return_false' );
    390394        add_action( 'deprecated_argument_trigger_error', '__return_false' );
    391         add_action( 'deprecated_hook_trigger_error',     '__return_false' );
    392         add_action( 'doing_it_wrong_trigger_error',      '__return_false' );
     395        add_action( 'deprecated_hook_trigger_error', '__return_false' );
     396        add_action( 'doing_it_wrong_trigger_error', '__return_false' );
    393397    }
    394398
     
    471475
    472476    function deprecated_function_run( $function ) {
    473         if ( ! in_array( $function, $this->caught_deprecated ) )
     477        if ( ! in_array( $function, $this->caught_deprecated ) ) {
    474478            $this->caught_deprecated[] = $function;
     479        }
    475480    }
    476481
    477482    function doing_it_wrong_run( $function ) {
    478         if ( ! in_array( $function, $this->caught_doing_it_wrong ) )
     483        if ( ! in_array( $function, $this->caught_doing_it_wrong ) ) {
    479484            $this->caught_doing_it_wrong[] = $function;
     485        }
    480486    }
    481487
     
    503509
    504510    function assertEqualFields( $object, $fields ) {
    505         foreach( $fields as $field_name => $field_value ) {
     511        foreach ( $fields as $field_name => $field_value ) {
    506512            if ( $object->$field_name != $field_value ) {
    507513                $this->fail();
     
    535541        $this->assertNotEmpty( $array );
    536542
    537         foreach( $array as $sub_array ) {
     543        foreach ( $array as $sub_array ) {
    538544            $this->assertTrue( is_array( $sub_array ) );
    539545            $this->assertNotEmpty( $sub_array );
     
    563569        // to run them more than once without very carefully clearing everything
    564570        $_GET = $_POST = array();
    565         foreach (array('query_string', 'id', 'postdata', 'authordata', 'day', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages', 'pagenow') as $v) {
    566             if ( isset( $GLOBALS[$v] ) ) unset( $GLOBALS[$v] );
    567         }
    568         $parts = parse_url($url);
    569         if (isset($parts['scheme'])) {
     571        foreach ( array( 'query_string', 'id', 'postdata', 'authordata', 'day', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages', 'pagenow' ) as $v ) {
     572            if ( isset( $GLOBALS[ $v ] ) ) {
     573                unset( $GLOBALS[ $v ] );
     574            }
     575        }
     576        $parts = parse_url( $url );
     577        if ( isset( $parts['scheme'] ) ) {
    570578            $req = isset( $parts['path'] ) ? $parts['path'] : '';
    571             if (isset($parts['query'])) {
     579            if ( isset( $parts['query'] ) ) {
    572580                $req .= '?' . $parts['query'];
    573581                // parse the url query vars into $_GET
    574                 parse_str($parts['query'], $_GET);
     582                parse_str( $parts['query'], $_GET );
    575583            }
    576584        } else {
     
    582590
    583591        $_SERVER['REQUEST_URI'] = $req;
    584         unset($_SERVER['PATH_INFO']);
     592        unset( $_SERVER['PATH_INFO'] );
    585593
    586594        self::flush_cache();
    587         unset($GLOBALS['wp_query'], $GLOBALS['wp_the_query']);
     595        unset( $GLOBALS['wp_query'], $GLOBALS['wp_the_query'] );
    588596        $GLOBALS['wp_the_query'] = new WP_Query();
    589         $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
     597        $GLOBALS['wp_query']     = $GLOBALS['wp_the_query'];
    590598
    591599        $public_query_vars  = $GLOBALS['wp']->public_query_vars;
    592600        $private_query_vars = $GLOBALS['wp']->private_query_vars;
    593601
    594         $GLOBALS['wp'] = new WP();
     602        $GLOBALS['wp']                     = new WP();
    595603        $GLOBALS['wp']->public_query_vars  = $public_query_vars;
    596604        $GLOBALS['wp']->private_query_vars = $private_query_vars;
     
    598606        _cleanup_query_vars();
    599607
    600         $GLOBALS['wp']->main($parts['query']);
     608        $GLOBALS['wp']->main( $parts['query'] );
    601609    }
    602610
     
    620628        }
    621629
    622         if ( WP_TESTS_FORCE_KNOWN_BUGS )
     630        if ( WP_TESTS_FORCE_KNOWN_BUGS ) {
    623631            return;
     632        }
    624633        $tickets = PHPUnit_Util_Test::getTickets( get_class( $this ), $this->getName( false ) );
    625634        foreach ( $tickets as $ticket ) {
     
    628637            } elseif ( 'Plugin' == substr( $ticket, 0, 6 ) ) {
    629638                $ticket = substr( $ticket, 6 );
    630                 if ( $ticket && is_numeric( $ticket ) )
     639                if ( $ticket && is_numeric( $ticket ) ) {
    631640                    $this->knownPluginBug( $ticket );
     641                }
    632642            }
    633643        }
     
    638648     */
    639649    function knownWPBug( $ticket_id ) {
    640         if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( $ticket_id, self::$forced_tickets ) )
     650        if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( $ticket_id, self::$forced_tickets ) ) {
    641651            return;
    642         if ( ! TracTickets::isTracTicketClosed( 'https://core.trac.wordpress.org', $ticket_id ) )
     652        }
     653        if ( ! TracTickets::isTracTicketClosed( 'https://core.trac.wordpress.org', $ticket_id ) ) {
    643654            $this->markTestSkipped( sprintf( 'WordPress Ticket #%d is not fixed', $ticket_id ) );
     655        }
    644656    }
    645657
     
    655667     */
    656668    function knownPluginBug( $ticket_id ) {
    657         if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( 'Plugin' . $ticket_id, self::$forced_tickets ) )
     669        if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( 'Plugin' . $ticket_id, self::$forced_tickets ) ) {
    658670            return;
    659         if ( ! TracTickets::isTracTicketClosed( 'https://plugins.trac.wordpress.org', $ticket_id ) )
     671        }
     672        if ( ! TracTickets::isTracTicketClosed( 'https://plugins.trac.wordpress.org', $ticket_id ) ) {
    660673            $this->markTestSkipped( sprintf( 'WordPress Plugin Ticket #%d is not fixed', $ticket_id ) );
     674        }
    661675    }
    662676
     
    679693    function temp_filename() {
    680694        $tmp_dir = '';
    681         $dirs = array( 'TMP', 'TMPDIR', 'TEMP' );
    682         foreach( $dirs as $dir )
    683             if ( isset( $_ENV[$dir] ) && !empty( $_ENV[$dir] ) ) {
     695        $dirs    = array( 'TMP', 'TMPDIR', 'TEMP' );
     696        foreach ( $dirs as $dir ) {
     697            if ( isset( $_ENV[ $dir ] ) && ! empty( $_ENV[ $dir ] ) ) {
    684698                $tmp_dir = $dir;
    685699                break;
    686700            }
     701        }
    687702        if ( empty( $tmp_dir ) ) {
    688703            $tmp_dir = '/tmp';
     
    701716     * @param string $prop,... Any number of WP_Query properties that are expected to be true for the current request.
    702717     */
    703     function assertQueryTrue(/* ... */) {
     718    function assertQueryTrue() {
    704719        global $wp_query;
    705         $all = array(
     720        $all  = array(
    706721            'is_404',
    707722            'is_admin',
     
    739754        }
    740755
    741         $passed = true;
     756        $passed  = true;
    742757        $message = '';
    743758
     
    748763                if ( ! $result ) {
    749764                    $message .= $query_thing . ' is false but is expected to be true. ' . PHP_EOL;
    750                     $passed = false;
     765                    $passed   = false;
    751766                }
    752             } else if ( $result ) {
     767            } elseif ( $result ) {
    753768                $message .= $query_thing . ' is true but is expected to be false. ' . PHP_EOL;
    754                 $passed = false;
     769                $passed   = false;
    755770            }
    756771        }
     
    790805
    791806        $iterator = new RecursiveDirectoryIterator( $dir );
    792         $objects = new RecursiveIteratorIterator( $iterator );
     807        $objects  = new RecursiveIteratorIterator( $iterator );
    793808        foreach ( $objects as $name => $object ) {
    794809            if ( is_file( $name ) ) {
     
    807822
    808823        $uploads = wp_upload_dir();
    809         $files = $this->files_in_dir( $uploads['basedir'] );
     824        $files   = $this->files_in_dir( $uploads['basedir'] );
    810825        return $files;
    811826    }
     
    836851     * Helper to Convert a microtime string into a float
    837852     */
    838     protected function _microtime_to_float($microtime ){
     853    protected function _microtime_to_float( $microtime ) {
    839854        $time_array = explode( ' ', $microtime );
    840855        return array_sum( $time_array );
     
    871886    }
    872887
    873     function _make_attachment($upload, $parent_post_id = 0) {
     888    function _make_attachment( $upload, $parent_post_id = 0 ) {
    874889        $type = '';
    875         if ( !empty($upload['type']) ) {
     890        if ( ! empty( $upload['type'] ) ) {
    876891            $type = $upload['type'];
    877892        } else {
    878893            $mime = wp_check_filetype( $upload['file'] );
    879             if ($mime)
     894            if ( $mime ) {
    880895                $type = $mime['type'];
     896            }
    881897        }
    882898
    883899        $attachment = array(
    884             'post_title' => basename( $upload['file'] ),
    885             'post_content' => '',
    886             'post_type' => 'attachment',
    887             'post_parent' => $parent_post_id,
     900            'post_title'     => basename( $upload['file'] ),
     901            'post_content'   => '',
     902            'post_type'      => 'attachment',
     903            'post_parent'    => $parent_post_id,
    888904            'post_mime_type' => $type,
    889             'guid' => $upload[ 'url' ],
     905            'guid'           => $upload['url'],
    890906        );
    891907
    892908        // Save the data
    893         $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent_post_id );
     909        $id = wp_insert_attachment( $attachment, $upload['file'], $parent_post_id );
    894910        wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
    895911        return $id;
     
    904920            $wpdb->posts,
    905921            array(
    906                 'post_modified' => $date,
     922                'post_modified'     => $date,
    907923                'post_modified_gmt' => $date,
    908924            ),
Note: See TracChangeset for help on using the changeset viewer.