Thiemo Mättig (WMDE) has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/266239

Change subject: Clean up Wikibase related code
......................................................................

Clean up Wikibase related code

I'm trying to fix as many code style issues as I can find. There is a
lot going on here. This fixes some warnings in my PHPStorm, gets rid
of duplicate code, inlines constants that do not make the code easier
to read, and more.

Change-Id: I02f446491509043f5d3e51e26e932f76c9ecb6cf
---
M MathFormatter.php
M MathInputCheckRestbase.php
M tests/MathFormatterTest.php
3 files changed, 39 insertions(+), 58 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Math 
refs/changes/39/266239/1

diff --git a/MathFormatter.php b/MathFormatter.php
index d7c4826..93802ef 100644
--- a/MathFormatter.php
+++ b/MathFormatter.php
@@ -2,7 +2,6 @@
 
 use DataValues\StringValue;
 use ValueFormatters\Exceptions\MismatchingDataValueTypeException;
-use ValueFormatters\FormattingException;
 use ValueFormatters\ValueFormatter;
 use Wikibase\Lib\SnakFormatter;
 
@@ -17,22 +16,25 @@
 
 class MathFormatter implements ValueFormatter {
 
+       /**
+        * @var string One of the SnakFormatter::FORMAT_... constants.
+        */
        private $format;
 
-       /*
+       /**
         * Loads format to distinguish the type of formatting
         *
-        * @param string $format
+        * @param string $format One of the SnakFormatter::FORMAT_... constants.
         *
         * @throws InvalidArgumentException
         */
        public function __construct( $format ) {
                switch ( $format ) {
-                       case ( SnakFormatter::FORMAT_HTML ):
-                       case ( SnakFormatter::FORMAT_HTML_DIFF ):
-                       case ( SnakFormatter::FORMAT_HTML_WIDGET ):
-                       case ( SnakFormatter::FORMAT_WIKI ):
-                       case ( SnakFormatter::FORMAT_PLAIN ):
+                       case SnakFormatter::FORMAT_PLAIN:
+                       case SnakFormatter::FORMAT_WIKI:
+                       case SnakFormatter::FORMAT_HTML:
+                       case SnakFormatter::FORMAT_HTML_DIFF:
+                       case SnakFormatter::FORMAT_HTML_WIDGET:
                                $this->format = $format;
                                break;
                        default:
@@ -40,44 +42,40 @@
                }
        }
 
-       /*
-        *
+       /**
         * @param StringValue $value
         *
+        * @throws MismatchingDataValueTypeException
         * @return string
-        * @throws \ValueFormatters\Exceptions\MismatchingDataValueTypeException
         */
        public function format( $value ) {
                if ( !( $value instanceof StringValue ) ) {
                        throw new MismatchingDataValueTypeException( 
'StringValue', get_class( $value ) );
                }
+
                $tex = $value->getValue();
 
                switch ( $this->format ) {
-                       case ( SnakFormatter::FORMAT_PLAIN ):
-                               return "$tex";
-                       case ( SnakFormatter::FORMAT_WIKI ):
+                       case SnakFormatter::FORMAT_PLAIN:
+                               return $tex;
+                       case SnakFormatter::FORMAT_WIKI:
                                return "<math>$tex</math>";
-                       case ( SnakFormatter::FORMAT_HTML ):
-                       case ( SnakFormatter::FORMAT_HTML_WIDGET ):
-                       case ( SnakFormatter::FORMAT_HTML_DIFF ):
+                       default:
                                $renderer = new MathMathML( $tex );
-                               if ( $renderer->checkTex() ) {
-                                       if ( $renderer->render() ) {
-                                               return 
$renderer->getHtmlOutput();
-                                       }
+                               if ( $renderer->checkTex() && 
$renderer->render() ) {
+                                       return $renderer->getHtmlOutput();
                                }
+
                                // TeX string is not valid or rendering failed
                                return $renderer->getLastError();
                }
        }
 
        /**
-        *
-        * @return format
+        * @return string One of the SnakFormatter::FORMAT_... constants.
         */
-
        public function getFormat() {
                return $this->format;
        }
+
 }
diff --git a/MathInputCheckRestbase.php b/MathInputCheckRestbase.php
index 0fd2bc5..eb70d66 100644
--- a/MathInputCheckRestbase.php
+++ b/MathInputCheckRestbase.php
@@ -9,8 +9,6 @@
  * @author Moritz Schubotz
  */
 
-use MediaWiki\Logger\LoggerFactory;
-
 class MathInputCheckRestbase extends MathInputCheck {
        private $restbaseInterface;
 
diff --git a/tests/MathFormatterTest.php b/tests/MathFormatterTest.php
index 9a499ce..6e19c3b 100644
--- a/tests/MathFormatterTest.php
+++ b/tests/MathFormatterTest.php
@@ -9,12 +9,8 @@
 use DataValues\NumberValue;
 
 class MathFormatterTest extends MediaWikiTestCase {
-       const SOME_TEX = "a^2+b^2=c^2";
-       const FORMAT_PLAIN = 'text/plain';
-       const FORMAT_HTML = 'text/html';
-       const FORMAT_XWIKI = 'text/x-wiki';
-       const FORMAT_UNKNOWN = 'unknown/unknown';
-       const FORMAT_VALUE = "";
+
+       const SOME_TEX = 'a^2+b^2=c^2';
 
        protected static $hasRestbase;
 
@@ -23,19 +19,12 @@
                self::$hasRestbase = $rbi->checkBackend( true );
        }
 
-       /**
-        * Sets up the fixture, for example, opens a network connection.
-        * This method is called before a test is executed.
-        */
        protected function setUp() {
                parent::setUp();
-               if ( !self::$hasRestbase ) {
-                       $this->markTestSkipped( "Can not connect to Restbase 
Math interface." );
-               }
-       }
 
-       protected function tearDown() {
-               parent::tearDown();
+               if ( !self::$hasRestbase ) {
+                       $this->markTestSkipped( 'Can not connect to Restbase 
Math interface.' );
+               }
        }
 
        /**
@@ -43,16 +32,16 @@
         * @covers MathFormatter::__construct()
         */
        public function testBasics() {
-               $formatter = new MathFormatter( self::FORMAT_PLAIN );
+               $formatter = new MathFormatter( 'text/plain' );
                // check if the format input was corretly passed to the class
-               $this->assertEquals( self::FORMAT_PLAIN, 
$formatter->getFormat(), 'test getFormat' );
+               $this->assertSame( 'text/plain', $formatter->getFormat(), 'test 
getFormat' );
        }
 
        /**
         * @expectedException 
ValueFormatters\Exceptions\MismatchingDataValueTypeException
         */
        public function testNotStringValue() {
-               $formatter = new MathFormatter( self::FORMAT_PLAIN );
+               $formatter = new MathFormatter( 'text/plain' );
                $formatter->format( new NumberValue( 0 ) );
        }
 
@@ -60,7 +49,7 @@
         * @expectedException 
ValueFormatters\Exceptions\MismatchingDataValueTypeException
         */
        public function testNullValue() {
-               $formatter = new MathFormatter( self::FORMAT_PLAIN );
+               $formatter = new MathFormatter( 'text/plain' );
                $formatter->format( null );
        }
 
@@ -68,33 +57,29 @@
         * @expectedException InvalidArgumentException
         */
        public function testUnknownFormat() {
-               $formatter = new MathFormatter( self::FORMAT_UNKNOWN );
+               new MathFormatter( 'unknown/unknown' );
        }
 
        public function testFormatPlain() {
-               $formatter = new MathFormatter( self::FORMAT_PLAIN );
+               $formatter = new MathFormatter( 'text/plain' );
                $value = new StringValue( self::SOME_TEX );
                $resultFormat = $formatter->format( $value );
-               $this->assertEquals( self::SOME_TEX, $resultFormat,
-                       'Results should be equal' );
-
+               $this->assertSame( self::SOME_TEX, $resultFormat );
        }
 
        public function testFormatHtml() {
-               $formatter = new MathFormatter( self::FORMAT_HTML );
+               $formatter = new MathFormatter( 'text/html' );
                $value = new StringValue( self::SOME_TEX );
                $resultFormat = $formatter->format( $value );
-               $this->assertContains( '</math>', $resultFormat,
-                       'Result must contain math-tag' );
+               $this->assertContains( '</math>', $resultFormat, 'Result must 
contain math-tag' );
        }
 
        public function testFormatXWiki() {
                $tex = self::SOME_TEX;
-               $formatter = new MathFormatter( self::FORMAT_XWIKI );
+               $formatter = new MathFormatter( 'text/x-wiki' );
                $value = new StringValue( self::SOME_TEX );
                $resultFormat = $formatter->format( $value );
-               $this->assertEquals( "<math>$tex</math>", $resultFormat,
-                       'Tex wasn\'t properly wrapped' );
-
+               $this->assertSame( "<math>$tex</math>", $resultFormat, 'Tex 
wasn\'t properly wrapped' );
        }
+
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I02f446491509043f5d3e51e26e932f76c9ecb6cf
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Math
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to