EBernhardson has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/337761 )

Change subject: Initial phan configuration for GeoData
......................................................................

Initial phan configuration for GeoData

While prepping the library update Elastica i thought it would be
convenient if some tests could notice newly missing classes/methods/etc
that were being used. Patch does the bare minimum to setup phan
to run against GeoData.

A few of the more strict configuration options have been disabled to
make this as simple as possible. Might be good to come back around
and fix them up:
* analyze_signature_compatability = false. Sovles for methods that
  extend a parent but don't declare phpdoc
* scalar_implicit_Cast = true. Solves for problems where
  int/float/string are used interchangably.
* suppress_issue_Types = [PhanDeprecatedFucntion]. GeoData uses a few
  deprecated things. These should be fixed, but for now this is ok-ish.

Once this is merged appropriate zuul configuration can be put together
so this runs on every commit.

Change-Id: I062704a3201ce67465c03f1a039af992b16c0fc1
Depends-On: I743f14f0e56c7f0546dfecd130925e15d63c7d64
---
M .gitignore
M includes/CoordinatesOutput.php
M includes/GeoData.body.php
M includes/Hooks.php
M includes/Searcher.php
M maintenance/updateIndexGranularity.php
A tests/phan/config.php
A tests/phan/issues/.gitkeep
8 files changed, 168 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/GeoData 
refs/changes/61/337761/1

diff --git a/.gitignore b/.gitignore
index 4bf4869..649e69f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,4 @@
 *.kate-swp
 .*.swp
 node_modules/
+tests/phan/issues
diff --git a/includes/CoordinatesOutput.php b/includes/CoordinatesOutput.php
index da8685f..afdbd92 100644
--- a/includes/CoordinatesOutput.php
+++ b/includes/CoordinatesOutput.php
@@ -8,10 +8,16 @@
  * Class that holds output of a parse opertion
  */
 class CoordinatesOutput {
+       /** @var bool */
        public $limitExceeded = false;
-       private $primary = false,
-               $secondary = [];
+       /** @var Coord|false */
+       private $primary = false;
+       /** @var Coord[] */
+       private $secondary = [];
 
+       /**
+        * @return int
+        */
        public function getCount() {
                return count( $this->secondary ) + ( $this->primary ? 1 : 0 );
        }
@@ -30,14 +36,23 @@
                $this->secondary[] = $c;
        }
 
+       /**
+        * @return Coord|false
+        */
        public function getPrimary() {
                return $this->primary;
        }
 
+       /**
+        * @return Coord[]
+        */
        public function getSecondary() {
                return $this->secondary;
        }
 
+       /**
+        * @return Coord[]
+        */
        public function getAll() {
                $res = $this->secondary;
                if ( $this->primary ) {
diff --git a/includes/GeoData.body.php b/includes/GeoData.body.php
index e0df02a..b508f85 100644
--- a/includes/GeoData.body.php
+++ b/includes/GeoData.body.php
@@ -2,6 +2,8 @@
 
 namespace GeoData;
 
+use IDatabase;
+use MediaWiki\MediaWikiServices;
 use Title;
 
 class GeoData {
@@ -27,7 +29,7 @@
         * @return Coord[]
         */
        public static function getAllCoordinates( $pageId, $conds = [], $dbType 
= DB_SLAVE ) {
-               $db = wfGetDB( $dbType );
+               $db = self::getDB( $dbType );
                $conds['gt_page_id'] = $pageId;
                $res = $db->select( 'geo_tags', Coord::getColumns(), $conds, 
__METHOD__ );
                $coords = [];
@@ -36,4 +38,14 @@
                }
                return $coords;
        }
+
+       /**
+        * @param int $dbType DB_MASTER or DB_SLAVE
+        * @return IDatabase
+        */
+       private static function getDB( $dbType ) {
+               return MediaWikiServices::getInstance()
+                       ->getDBLoadBalancer()
+                       ->getConnection( $dbType );
+       }
 }
diff --git a/includes/Hooks.php b/includes/Hooks.php
index 4ac1c91..dc42ac8 100644
--- a/includes/Hooks.php
+++ b/includes/Hooks.php
@@ -81,7 +81,7 @@
         */
        public static function onParserFirstCallInit( &$parser ) {
                $parser->setFunctionHook( 'coordinates',
-                       [ new CoordinatesParserFunction( $parser ), 
'coordinates' ],
+                       [ new CoordinatesParserFunction(), 'coordinates' ],
                        Parser::SFH_OBJECT_ARGS
                );
        }
@@ -261,9 +261,10 @@
         */
        public static function onSearchIndexFields( array &$fields, 
SearchEngine $engine ) {
                global $wgGeoDataUseCirrusSearch, $wgGeoDataBackend;
-               if ( $engine instanceof \CirrusSearch
-                       && ( $wgGeoDataUseCirrusSearch || $wgGeoDataBackend  == 
'elastic' )
-               ) {
+               if ( !$wgGeoDataUseCirrusSearch && $wgGeoDataBackend  !== 
'elastic' ) {
+                       return;
+               }
+               if ( $engine instanceof \CirrusSearch ) {
                        /**
                         * @var \CirrusSearch $engine
                         */
diff --git a/includes/Searcher.php b/includes/Searcher.php
index 8642325..852feb6 100644
--- a/includes/Searcher.php
+++ b/includes/Searcher.php
@@ -17,6 +17,7 @@
        public function __construct( User $user = null ) {
                /** @var SearchConfig $config */
                $config = ConfigFactory::getDefaultInstance()->makeConfig( 
'CirrusSearch' );
+               /** @suppress PhanTypeMismatchArgument */
                $connection = new \CirrusSearch\Connection( $config );
 
                parent::__construct( $connection, $user, 0 );
diff --git a/maintenance/updateIndexGranularity.php 
b/maintenance/updateIndexGranularity.php
index ee25935..541bdf9 100644
--- a/maintenance/updateIndexGranularity.php
+++ b/maintenance/updateIndexGranularity.php
@@ -22,7 +22,7 @@
                        $this->error( 'Please install GeoData properly', true );
                }
                $id = 0;
-               $dbw = wfGetDB( DB_MASTER );
+               $dbw = $this->getDB( DB_MASTER );
                do {
                        $ids = [];
 
@@ -50,6 +50,16 @@
                        wfWaitForSlaves();
                } while ( count( $ids ) === self::BATCH_SIZE );
        }
+
+       /**
+        * @param int $dbType DB_MASTER or DB_SLAVE
+        * @return IDatabase
+        */
+       private function getDB( $dbType ) {
+               return MediaWiki\MediaWikiServices::getInstance()
+                       ->getDBLoadBalancer()
+                       ->getConnection( $dbType );
+       }
 }
 
 $maintClass = 'UpdateIndexGranularity';
diff --git a/tests/phan/config.php b/tests/phan/config.php
new file mode 100644
index 0000000..a7eae1c
--- /dev/null
+++ b/tests/phan/config.php
@@ -0,0 +1,120 @@
+<?php
+
+/**
+ * This configuration will be read and overlaid on top of the
+ * default configuration. Command line arguments will be applied
+ * after this file is read.
+ *
+ * @see src/Phan/Config.php
+ * See Config for all configurable options.
+ */
+return [
+       // A list of directories that should be parsed for class and
+       // method information. After excluding the directories
+       // defined in exclude_analysis_directory_list, the remaining
+       // files will be statically analyzed for errors.
+       //
+       // Thus, both first-party and third-party code being used by
+       // your application should be included in this list.
+       'directory_list' => [
+               '.',
+               '../../includes',
+               '../../languages',
+               '../../maintenance',
+               '../../vendor',
+               '../../extensions/Elastica',
+               '../../extensions/CirrusSearch',
+       ],
+
+       // A directory list that defines files that will be excluded
+       // from static analysis, but whose class and method
+       // information should be included.
+       //
+       // Generally, you'll want to include the directories for
+       // third-party code (such as "vendor/") in this list.
+       //
+       // n.b.: If you'd like to parse but not analyze 3rd
+       //         party code, directories containing that code
+       //         should be added to the `directory_list` as
+       //         to `excluce_analysis_directory_list`.
+       "exclude_analysis_directory_list" => [
+               './tests/',
+               '../../includes',
+               '../../languages',
+               '../../maintenance',
+               '../../vendor',
+               '../../extensions/Elastica',
+               '../../extensions/CirrusSearch',
+       ],
+
+       // Backwards Compatibility Checking. This is slow
+       // and expensive, but you should consider running
+       // it before upgrading your version of PHP to a
+       // new version that has backward compatibility
+       // breaks.
+       'backward_compatibility_checks' => false,
+
+       // Run a quick version of checks that takes less
+       // time at the cost of not running as thorough
+       // an analysis. You should consider setting this
+       // to true only when you wish you had more issues
+       // to fix in your code base.
+       'quick_mode' => false,
+
+       // If enabled, check all methods that override a
+       // parent method to make sure its signature is
+       // compatible with the parent's. This check
+       // can add quite a bit of time to the analysis.
+       //
+       // GeoData is too lazy to add appropriate annotations
+       // to overridden methods for actual compatability
+       'analyze_signature_compatibility' => false,
+
+       // The minimum severity level to report on. This can be
+       // set to Issue::SEVERITY_LOW, Issue::SEVERITY_NORMAL or
+       // Issue::SEVERITY_CRITICAL. Setting it to only
+       // critical issues is a good place to start on a big
+       // sloppy mature code base.
+       'minimum_severity' => 0,
+
+       // If true, missing properties will be created when
+       // they are first seen. If false, we'll report an
+       // error message if there is an attempt to write
+       // to a class property that wasn't explicitly
+       // defined.
+       //
+       // GeoData stuffs extra properties into the ParserOutput
+       // For now just let it be (and miss unrelated typo's).
+       'allow_missing_properties' => true,
+
+       // Allow null to be cast as any type and for any
+       // type to be cast to null. Setting this to false
+       // will cut down on false positives.
+       'null_casts_as_any_type' => false,
+
+       // If enabled, scalars (int, float, bool, string, null)
+       // are treated as if they can cast to each other.
+       //
+       // GeoData plays fast and loose with float->string, etc
+       // conversions
+       'scalar_implicit_cast' => true,
+
+       // If true, seemingly undeclared variables in the global
+       // scope will be ignored. This is useful for projects
+       // with complicated cross-file globals that you have no
+       // hope of fixing.
+       'ignore_undeclared_variables_in_global_scope' => true,
+
+       // Add any issue types (such as 'PhanUndeclaredMethod')
+       // to this black-list to inhibit them from being reported.
+       'suppress_issue_types' => [
+               // GeoData references a bunch of deprecated code. Fix later.
+               'PhanDeprecatedFunction',
+       ],
+
+       // If empty, no filter against issues types will be applied.
+       // If this white-list is non-empty, only issues within the list
+       // will be emitted by Phan.
+       'whitelist_issue_types' => [
+       ],
+];
diff --git a/tests/phan/issues/.gitkeep b/tests/phan/issues/.gitkeep
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/phan/issues/.gitkeep

-- 
To view, visit https://gerrit.wikimedia.org/r/337761
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I062704a3201ce67465c03f1a039af992b16c0fc1
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/GeoData
Gerrit-Branch: master
Gerrit-Owner: EBernhardson <ebernhard...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to