Просмотр исходного кода

Merge pull request #213 from alcaeus/php-cs-fix

Run phpcs in travis-ci and fix checkstyles
Andreas 8 лет назад
Родитель
Сommit
b165b284fa

+ 1 - 0
.gitignore

@@ -1,3 +1,4 @@
 composer.lock
+.phpcs-cache
 vendor/
 tests/scripts/

+ 7 - 1
.travis.yml

@@ -11,7 +11,7 @@ php:
   - 7.1
 
 env:
-  matrix:
+  global:
     - DRIVER_VERSION="stable"
 
 addons:
@@ -58,6 +58,12 @@ jobs:
             - "mongodb-upstart"
           packages: ['mongodb-org-server']
 
+    - stage: Code Quality
+      env: CODING_STANDARDS
+      php: 7.1
+      script:
+        - ./vendor/bin/phpcs
+
 cache:
   directories:
     - $HOME/.composer/cache

+ 2 - 1
composer.json

@@ -15,7 +15,8 @@
         "mongodb/mongodb": "^1.0.1"
     },
     "require-dev": {
-        "phpunit/phpunit": "^5.7 || ^6.0"
+        "phpunit/phpunit": "^5.7 || ^6.0",
+        "squizlabs/php_codesniffer": "^3.2"
     },
     "provide": {
         "ext-mongo": "1.6.14"

+ 9 - 9
lib/Mongo/MongoClient.php

@@ -32,13 +32,13 @@ class MongoClient
     use Helper\WriteConcern;
 
     const VERSION = '1.6.12';
-    const DEFAULT_HOST = "localhost" ;
-    const DEFAULT_PORT = 27017 ;
-    const RP_PRIMARY = "primary" ;
-    const RP_PRIMARY_PREFERRED = "primaryPreferred" ;
-    const RP_SECONDARY = "secondary" ;
-    const RP_SECONDARY_PREFERRED = "secondaryPreferred" ;
-    const RP_NEAREST = "nearest" ;
+    const DEFAULT_HOST = "localhost";
+    const DEFAULT_PORT = 27017;
+    const RP_PRIMARY = "primary";
+    const RP_PRIMARY_PREFERRED = "primaryPreferred";
+    const RP_SECONDARY = "secondary";
+    const RP_SECONDARY_PREFERRED = "secondaryPreferred";
+    const RP_NEAREST = "nearest";
 
     /**
      * @var bool
@@ -94,7 +94,7 @@ class MongoClient
 
         $this->server = $server;
         if (false === strpos($this->server, 'mongodb://')) {
-            $this->server = 'mongodb://'.$this->server;
+            $this->server = 'mongodb://' . $this->server;
         }
         $this->client = new Client($this->server, $options, $driverOptions);
         $info = $this->client->__debugInfo();
@@ -352,7 +352,7 @@ class MongoClient
     /**
      * @return array
      */
-    function __sleep()
+    public function __sleep()
     {
         return [
             'connected', 'status', 'server', 'persistent'

+ 3 - 3
lib/Mongo/MongoCollection.php

@@ -888,14 +888,14 @@ class MongoCollection
         $command = [
             'group' => [
                 'ns' => $this->name,
-                '$reduce' => (string)$reduce,
+                '$reduce' => (string) $reduce,
                 'initial' => $initial,
                 'cond' => $condition,
             ],
         ];
 
         if ($keys instanceof MongoCode) {
-            $command['group']['$keyf'] = (string)$keys;
+            $command['group']['$keyf'] = (string) $keys;
         } else {
             $command['group']['key'] = $keys;
         }
@@ -904,7 +904,7 @@ class MongoCollection
         }
         if (array_key_exists('finalize', $condition)) {
             if ($condition['finalize'] instanceof MongoCode) {
-                $condition['finalize'] = (string)$condition['finalize'];
+                $condition['finalize'] = (string) $condition['finalize'];
             }
             $command['group']['finalize'] = $condition['finalize'];
         }

+ 3 - 1
lib/Mongo/MongoDB.php

@@ -154,7 +154,9 @@ class MongoDB
                     'info' => isset($info['info']) ? (array) $info['info'] : null,
                     'idIndex' => isset($info['idIndex']) ? (array) $info['idIndex'] : null,
                 ],
-                function ($item) { return $item !== null; }
+                function ($item) {
+                    return $item !== null;
+                }
             );
         };
 

+ 1 - 1
lib/Mongo/MongoGridFS.php

@@ -207,7 +207,7 @@ class MongoGridFS extends MongoCollection
         try {
             $file = $this->insertFile($record, $options);
         } catch (MongoException $e) {
-            throw new MongoGridFSException('Could not store file: '. $e->getMessage(), $e->getCode(), $e);
+            throw new MongoGridFSException('Could not store file: ' . $e->getMessage(), $e->getCode(), $e);
         }
 
         try {

+ 1 - 1
lib/Mongo/MongoGridFSCursor.php

@@ -68,6 +68,6 @@ class MongoGridFSCursor extends MongoCursor
     public function key()
     {
         $file = $this->current();
-        return ($file !== null) ? (string)$file->file['_id'] : null;
+        return ($file !== null) ? (string) $file->file['_id'] : null;
     }
 }

+ 1 - 1
lib/Mongo/MongoWriteBatch.php

@@ -83,7 +83,7 @@ class MongoWriteBatch
     public function add($item)
     {
         if (is_object($item)) {
-            $item = (array)$item;
+            $item = (array) $item;
         }
 
         $this->validate($item);

+ 45 - 0
phpcs.xml.dist

@@ -0,0 +1,45 @@
+<?xml version="1.0"?>
+<ruleset>
+    <arg name="basepath" value="."/>
+    <arg name="extensions" value="php"/>
+    <arg name="parallel" value="80"/>
+    <arg name="cache" value=".phpcs-cache"/>
+    <arg name="colors" />
+
+    <!-- Ignore warnings and show progress of the run -->
+    <arg value="np"/>
+
+    <file>lib</file>
+    <file>tests</file>
+
+    <rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
+        <exclude-pattern>*/tests/*</exclude-pattern>
+    </rule>
+
+    <!-- Import PSR-2 coding standard (base) -->
+    <rule ref="PSR2">
+        <!-- Using \Doctrine\Sniffs\Spacing\ControlStructureSniff instead -->
+        <exclude name="PSR2.ControlStructures.ControlStructureSpacing"/>
+    </rule>
+
+    <!-- Import PSR-2 coding standard (base) -->
+    <rule ref="PSR1.Classes.ClassDeclaration.MissingNamespace">
+        <exclude-pattern>*/lib/Mongo/*</exclude-pattern>
+    </rule>
+
+    <!-- Disallow else if in favour elseif -->
+    <rule ref="PSR2.ControlStructures.ElseIfDeclaration.NotAllowed">
+        <type>error</type>
+    </rule>
+
+    <!-- Force whitespace after a type cast -->
+    <rule ref="Generic.Formatting.SpaceAfterCast" />
+
+    <!-- Force whitespace before and after concatenation -->
+    <rule ref="Squiz.Strings.ConcatenationSpacing">
+        <properties>
+            <property name="spacing" value="1"/>
+            <property name="ignoreNewlines" value="true"/>
+        </properties>
+    </rule>
+</ruleset>

+ 1 - 1
tests/Alcaeus/MongoDbAdapter/Mongo/MongoBinDataTest.php

@@ -18,7 +18,7 @@ class MongoBinDataTest extends TestCase
         $this->assertAttributeSame(self::GUID, 'bin', $bin);
         $this->assertAttributeSame(\MongoBinData::FUNC, 'type', $bin);
 
-        $this->assertSame('<Mongo Binary Data>', (string)$bin);
+        $this->assertSame('<Mongo Binary Data>', (string) $bin);
 
         return $bin;
     }

+ 1 - 1
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCodeTest.php

@@ -16,7 +16,7 @@ class MongoCodeTest extends TestCase
         $this->assertAttributeSame('code', 'code', $code);
         $this->assertAttributeSame(['scope' => 'bleh'], 'scope', $code);
 
-        $this->assertSame('code', (string)$code);
+        $this->assertSame('code', (string) $code);
 
         return $code;
     }

+ 2 - 1
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php

@@ -1061,7 +1061,8 @@ class MongoCollectionTest extends TestCase
         $this->assertAttributeSame('foo', 'foo', $object);
     }
 
-    public function testSavingShouldReplaceTheWholeDocument() {
+    public function testSavingShouldReplaceTheWholeDocument()
+    {
         $id = '54203e08d51d4a1f868b456e';
         $collection = $this->getCollection();
 

+ 6 - 3
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCursorTest.php

@@ -200,7 +200,8 @@ class MongoCursorTest extends TestCase
 
     public static function getCursorOptions()
     {
-        function getMissingOptionCallback($optionName) {
+        function getMissingOptionCallback($optionName)
+        {
             return function ($value) use ($optionName) {
                 return
                     is_array($value) &&
@@ -208,7 +209,8 @@ class MongoCursorTest extends TestCase
             };
         }
 
-        function getBasicCheckCallback($expected, $optionName) {
+        function getBasicCheckCallback($expected, $optionName)
+        {
             return function ($value) use ($expected, $optionName) {
                 return
                     is_array($value) &&
@@ -217,7 +219,8 @@ class MongoCursorTest extends TestCase
             };
         }
 
-        function getModifierCheckCallback($expected, $modifierName) {
+        function getModifierCheckCallback($expected, $modifierName)
+        {
             return function ($value) use ($expected, $modifierName) {
                 return
                     is_array($value) &&

+ 0 - 1
tests/Alcaeus/MongoDbAdapter/Mongo/MongoDBTest.php

@@ -180,7 +180,6 @@ class MongoDBTest extends TestCase
 
         $this->assertSame(ReadPreference::RP_SECONDARY, $readPreference->getMode());
         $this->assertSame([['a' => 'b']], $readPreference->getTagSets());
-
     }
 
     public function testReadPreferenceIsInherited()

+ 2 - 1
tests/Alcaeus/MongoDbAdapter/Mongo/MongoDateTest.php

@@ -17,7 +17,8 @@ class MongoDateTest extends TestCase
         ini_set("date.timezone", "UTC");
 
         // Today at 8h 8m 8s
-        $timestamp = mktime (8, 8, 8); $date = new \MongoDate($timestamp);
+        $timestamp = mktime(8, 8, 8);
+        $date = new \MongoDate($timestamp);
 
         $this->assertSame('08:08:08', $date->toDateTime()->format("H:i:s"));
 

+ 1 - 1
tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSCursorTest.php

@@ -25,7 +25,7 @@ class MongoGridFSCursorTest extends TestCase
         $cursor = $gridfs->find(['filename' => 'foo.txt']);
         $this->assertCount(1, $cursor);
         foreach ($cursor as $key => $value) {
-            $this->assertSame((string)$id, $key);
+            $this->assertSame((string) $id, $key);
             $this->assertInstanceOf('MongoGridFSFile', $value);
             $this->assertSame('foo', $value->getBytes());
 

+ 4 - 4
tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSTest.php

@@ -177,7 +177,7 @@ class MongoGridFSTest extends TestCase
         $this->assertObjectHasAttribute('filename', $record);
         $this->assertAttributeSame($filename, 'filename', $record);
 
-        $numberOfChunks = (int)ceil($size / 100);
+        $numberOfChunks = (int) ceil($size / 100);
         $this->assertSame($numberOfChunks, $newChunksCollection->count());
         $expectedContent = substr(file_get_contents(__FILE__), 0, 100);
 
@@ -222,7 +222,7 @@ class MongoGridFSTest extends TestCase
         $this->assertObjectHasAttribute('filename', $record);
         $this->assertAttributeSame('test.php', 'filename', $record);
 
-        $numberOfChunks = (int)ceil($size / 100);
+        $numberOfChunks = (int) ceil($size / 100);
         $this->assertSame($numberOfChunks, $newChunksCollection->count());
         $expectedContent = substr(file_get_contents(__FILE__), 0, 100);
 
@@ -273,7 +273,7 @@ class MongoGridFSTest extends TestCase
         $this->assertObjectHasAttribute('filename', $record);
         $this->assertAttributeSame('test.php', 'filename', $record);
 
-        $numberOfChunks = (int)ceil($size / 100);
+        $numberOfChunks = (int) ceil($size / 100);
         $this->assertSame($numberOfChunks, $newChunksCollection->count());
     }
 
@@ -332,7 +332,7 @@ class MongoGridFSTest extends TestCase
         $this->assertSame(1, $newCollection->count());
 
         $size = filesize(__FILE__);
-        $numberOfChunks = (int)ceil($size / 100);
+        $numberOfChunks = (int) ceil($size / 100);
         $this->assertSame($numberOfChunks, $newChunksCollection->count());
     }
 

+ 2 - 1
tests/Alcaeus/MongoDbAdapter/Mongo/MongoLogTest.php

@@ -8,7 +8,8 @@ class MongoLogTest extends Testcase
 {
     public function testSetCallback()
     {
-        $foo = function() {};
+        $foo = function () {
+        };
         $this->assertTrue(\MongoLog::setCallback($foo));
         $this->assertSame($foo, \MongoLog::getCallback());
     }

+ 1 - 1
tests/Alcaeus/MongoDbAdapter/Mongo/MongoTimestampTest.php

@@ -63,6 +63,6 @@ class MongoTimestampTest extends TestCase
         $bsonTimestamp = new \MongoDB\BSON\Timestamp(12345, 67890);
         $timestamp = new \MongoTimestamp(67890, 12345);
 
-        $this->assertSame((string) $bsonTimestamp, (string)$timestamp->toBSONType());
+        $this->assertSame((string) $bsonTimestamp, (string) $timestamp->toBSONType());
     }
 }

+ 1 - 1
tests/Alcaeus/MongoDbAdapter/TestCase.php

@@ -139,7 +139,7 @@ abstract class TestCase extends BaseTestCase
             /* command not found */
             if ($e->getCode() == 59) {
                 $this->markTestSkipped(
-                  'This test require the mongo daemon to be started with the test flag: --setParameter enableTestCommands=1'
+                    'This test require the mongo daemon to be started with the test flag: --setParameter enableTestCommands=1'
                 );
             }
         }