Verify parameter for MapCacheLRU::has() can be passed to array_key_exists()
authorMark A. Hershberger <mah@everybody.org>
Sun, 30 Nov 2014 20:16:04 +0000 (15:16 -0500)
committerChad Horohoe <chadh@wikimedia.org>
Mon, 23 Mar 2015 22:26:59 +0000 (15:26 -0700)
This prevents warnings from PHP from array_key_exists().

Also make sure Title::newFromText throws an exception before it can trigger this.

Bug: T76305
Change-Id: I2b36b7a3b96b37e29fe06f69c13a185b3ec592a7

includes/Title.php
includes/libs/MapCacheLRU.php
tests/phpunit/includes/SampleTest.php

index 921538b..9868b2e 100644 (file)
@@ -256,12 +256,12 @@ class Title {
         *   by a prefix.  If you want to force a specific namespace even if
         *   $text might begin with a namespace prefix, use makeTitle() or
         *   makeTitleSafe().
-        * @throws MWException
+        * @throws InvalidArgumentException
         * @return Title|null Title or null on an error.
         */
        public static function newFromText( $text, $defaultNamespace = NS_MAIN ) {
-               if ( is_object( $text ) ) {
-                       throw new MWException( 'Title::newFromText given an object' );
+               if ( !is_string( $text ) ) {
+                       throw new InvalidArgumentException( 'Title::newFromText given something that isn\'t a string' );
                }
 
                $cache = self::getTitleCache();
index 0b6db32..b0cb078 100644 (file)
@@ -74,7 +74,11 @@ class MapCacheLRU {
         * @return bool
         */
        public function has( $key ) {
-               return array_key_exists( $key, $this->cache );
+               if ( is_string( $key ) || is_integer( $key ) ) {
+                       return array_key_exists( $key, $this->cache );
+               }
+               wfWarn( __METHOD__ . ": Key passed isn't a string or an integer.", 2 );
+               return false;
        }
 
        /**
index 2585811..c5944d1 100644 (file)
@@ -97,7 +97,7 @@ class TestSample extends MediaWikiLangTestCase {
 
        // @codingStandardsIgnoreStart Ignore long line warning
        /**
-        * @expectedException MWException object
+        * @expectedException InvalidArgumentException
         * See http://phpunit.de/manual/3.7/en/appendixes.annotations.html#appendixes.annotations.expectedException
         */
        // @codingStandardsIgnoreEnd