Skip to content

Commit 67ffd33

Browse files
committed
Merge pull request #668
2 parents a72cbe3 + b00ceb1 commit 67ffd33

File tree

149 files changed

+2035
-1406
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

149 files changed

+2035
-1406
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ vendor/
77
phpunit.phar
88
phpunit.xml
99
.phpunit.result.cache
10+
11+
# phpcs
12+
.phpcs-cache
13+
phpcs.xml

.phpcs/autoload.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
// Since doctrine/coding-standard requires PHP 7, we can't add it as a dependency
4+
// yet. This autoload file adds more information to the phpcs error message,
5+
// telling the user how they can fix the error presented to them by phpcs.
6+
if (! file_exists(__DIR__ . '/../vendor/doctrine/coding-standard')) {
7+
echo <<<ERRORMESSAGE
8+
==============================================================================
9+
ERROR: Doctrine coding standard is not installed. To rectify this, please run:
10+
composer require --dev doctrine/coding-standard=^6.0
11+
==============================================================================
12+
13+
14+
ERRORMESSAGE;
15+
}

.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ jobs:
2525

2626
- stage: Smoke Testing
2727
php: "7.3"
28+
env:
29+
- CHECKS=phpunit
30+
- stage: Smoke Testing
31+
php: "7.1"
32+
before_install: []
33+
before_script:
34+
- pecl install -f mongodb-${DRIVER_VERSION}
35+
- composer require --no-update doctrine/coding-standard=^6.0
36+
- composer install --no-interaction --no-progress --no-suggest ${COMPOSER_OPTIONS}
37+
script: vendor/bin/phpcs
38+
after_script: []
39+
after_failure: []
40+
env:
41+
- CHECKS=phpcs
2842

2943
# Test remaining supported PHP versions
3044
- stage: Test

CONTRIBUTING.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,31 @@ test suite. In addition to various PHPUnit options, it defines required
3535
this configuration by creating your own `phpunit.xml` file based on the
3636
`phpunit.xml.dist` file we provide.
3737

38+
## Checking coding standards
39+
40+
The library's code is checked using [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer),
41+
which is installed as a development dependency by Composer. Due to the PHP
42+
requirement, the base version of the coding standard is not installed and needs
43+
to be added manually if you plan to contributing code:
44+
45+
```
46+
$ composer require --dev doctrine/coding-standard=^6.0
47+
```
48+
49+
Once the coding standard has been installed, you can check the code for style
50+
errors:
51+
52+
53+
```
54+
$ vendor/bin/phpcs
55+
```
56+
57+
To automatically fix all fixable errors, use the `phpcbf` binary:
58+
59+
```
60+
$ vendor/bin/phpcbf
61+
```
62+
3863
## Documentation
3964

4065
Documentation for the library lives in the `docs/` directory and is built with

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"require-dev": {
1919
"phpunit/phpunit": "^5.7.27 || ^6.4 || ^8.3",
2020
"sebastian/comparator": "^1.0 || ^2.0 || ^3.0",
21+
"squizlabs/php_codesniffer": "^3.4",
2122
"symfony/phpunit-bridge": "^4.4@dev"
2223
},
2324
"autoload": {

phpcs.xml.dist

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?xml version="1.0"?>
2+
<ruleset>
3+
<arg name="basepath" value="."/>
4+
<arg name="extensions" value="php"/>
5+
<arg name="parallel" value="80"/>
6+
<arg name="cache" value=".phpcs-cache"/>
7+
<arg name="colors" />
8+
9+
<!-- Ignore warnings, show progress of the run, and show sniff names -->
10+
<arg value="nps"/>
11+
12+
<autoload>.phpcs/autoload.php</autoload>
13+
14+
<file>.phpcs</file>
15+
<file>src</file>
16+
<file>tests</file>
17+
18+
<rule ref="Doctrine">
19+
<!-- Exclude sniffs that require newer PHP versions -->
20+
<!-- Available with PHP 7.0 -->
21+
<exclude name="SlevomatCodingStandard.TypeHints.DeclareStrictTypes" />
22+
<!-- In addition to requiring PHP 7.0, this sniff will cause a significant amount of BC breaks. Proceed with caution! -->
23+
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration" />
24+
<exclude name="SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly" />
25+
<exclude name="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator" />
26+
27+
<!-- Available with PHP 7.1 -->
28+
<exclude name="SlevomatCodingStandard.Classes.ClassConstantVisibility" />
29+
<exclude name="SlevomatCodingStandard.PHP.ShortList.LongListUsed" />
30+
<exclude name="SlevomatCodingStandard.TypeHints.NullableTypeForNullDefaultValue" />
31+
32+
<!-- No statement alignment so far -->
33+
<exclude name="Generic.Formatting.MultipleStatementAlignment" />
34+
35+
<!-- Class naming sniffs are excluded to preserve BC -->
36+
<exclude name="SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming" />
37+
<exclude name="SlevomatCodingStandard.Classes.SuperfluousExceptionNaming" />
38+
<exclude name="SlevomatCodingStandard.Classes.SuperfluousInterfaceNaming" />
39+
<exclude name="SlevomatCodingStandard.Classes.SuperfluousTraitNaming" />
40+
41+
<!-- Forbid useless annotations - Git and LICENCE file provide more accurate information -->
42+
<!-- Disable forbidden annotation sniff as excluding @api from the list doesn't work -->
43+
<exclude name="SlevomatCodingStandard.Commenting.ForbiddenAnnotations.AnnotationForbidden" />
44+
45+
<!-- Keep long typehints (for now) -->
46+
<exclude name="SlevomatCodingStandard.PHP.TypeCast.InvalidCastUsed" />
47+
<exclude name="SlevomatCodingStandard.TypeHints.LongTypeHints" />
48+
49+
<!-- Don't require a full stop after @throws tags -->
50+
<exclude name="Squiz.Commenting.FunctionComment.ThrowsNoFullStop" />
51+
52+
<!-- Disable some sniffs as they can cause functional changes. These will be enabled later -->
53+
<exclude name="Generic.PHP.ForbiddenFunctions.FoundWithAlternative" />
54+
<exclude name="SlevomatCodingStandard.Classes.UnusedPrivateElements" />
55+
<exclude name="SlevomatCodingStandard.ControlStructures.DisallowYodaComparison" />
56+
<exclude name="SlevomatCodingStandard.ControlStructures.EarlyExit" />
57+
<exclude name="SlevomatCodingStandard.ControlStructures.UselessIfConditionWithReturn" />
58+
<exclude name="SlevomatCodingStandard.Functions.StaticClosure" />
59+
<exclude name="SlevomatCodingStandard.Functions.UnusedInheritedVariablePassedToClosure" />
60+
<exclude name="SlevomatCodingStandard.Operators.DisallowEqualOperators" />
61+
62+
<!-- These sniffs cause a large diff, so enable them in separate steps -->
63+
<exclude name="SlevomatCodingStandard.Commenting.DocCommentSpacing.IncorrectAnnotationsGroup" />
64+
<exclude name="SlevomatCodingStandard.Commenting.RequireOneLinePropertyDocComment" />
65+
<exclude name="Squiz.Strings.DoubleQuoteUsage" />
66+
67+
<!-- Sniff currently breaks, see https://github.com/slevomat/coding-standard/issues/727 -->
68+
<exclude name="SlevomatCodingStandard.Namespaces.NamespaceSpacing" />
69+
</rule>
70+
71+
<!-- Change use statement sorting to be compatible with PSR-12 -->
72+
<rule ref="SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses">
73+
<properties>
74+
<property name="psr12Compatible" value="true"/>
75+
</properties>
76+
</rule>
77+
78+
<!-- Forbid fully qualified names even for colliding names -->
79+
<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly">
80+
<properties>
81+
<property name="allowFallbackGlobalConstants" value="false"/>
82+
<property name="allowFallbackGlobalFunctions" value="false"/>
83+
<property name="allowFullyQualifiedGlobalClasses" value="false"/>
84+
<property name="allowFullyQualifiedGlobalConstants" value="false"/>
85+
<property name="allowFullyQualifiedGlobalFunctions" value="false"/>
86+
<property phpcs-only="true" name="allowFullyQualifiedNameForCollidingClasses" value="false"/>
87+
<property phpcs-only="true" name="allowFullyQualifiedNameForCollidingConstants" value="false"/>
88+
<property phpcs-only="true" name="allowFullyQualifiedNameForCollidingFunctions" value="false"/>
89+
<property name="searchAnnotations" value="true"/>
90+
</properties>
91+
</rule>
92+
93+
<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
94+
<exclude-pattern>/src/GridFS/StreamWrapper</exclude-pattern>
95+
<exclude-pattern>/tests/DocumentationExamplesTest.php</exclude-pattern>
96+
</rule>
97+
98+
<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
99+
<exclude-pattern>/tests/Compat/PolyfillAssertTrait.php</exclude-pattern>
100+
</rule>
101+
</ruleset>

src/BulkWriteResult.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ class BulkWriteResult
3030
private $isAcknowledged;
3131

3232
/**
33-
* Constructor.
34-
*
3533
* @param WriteResult $writeResult
3634
* @param mixed[] $insertedIds
3735
*/

src/ChangeStream.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717

1818
namespace MongoDB;
1919

20+
use Iterator;
2021
use MongoDB\Driver\CursorId;
2122
use MongoDB\Driver\Exception\ConnectionException;
2223
use MongoDB\Driver\Exception\RuntimeException;
2324
use MongoDB\Driver\Exception\ServerException;
2425
use MongoDB\Exception\ResumeTokenException;
2526
use MongoDB\Model\ChangeStreamIterator;
26-
use Iterator;
27+
use function call_user_func;
28+
use function in_array;
2729

2830
/**
2931
* Iterator for a change stream.
@@ -57,8 +59,6 @@ class ChangeStream implements Iterator
5759
private $hasAdvanced = false;
5860

5961
/**
60-
* Constructor.
61-
*
6262
* @internal
6363
* @param ChangeStreamIterator $iterator
6464
* @param callable $resumeCallable
@@ -109,6 +109,7 @@ public function key()
109109
if ($this->valid()) {
110110
return $this->key;
111111
}
112+
112113
return null;
113114
}
114115

@@ -167,7 +168,7 @@ private function isResumableError(RuntimeException $exception)
167168
return true;
168169
}
169170

170-
if ( ! $exception instanceof ServerException) {
171+
if (! $exception instanceof ServerException) {
171172
return false;
172173
}
173174

@@ -202,7 +203,7 @@ private function onIteration($incrementKey)
202203

203204
/* Return early if there is not a current result. Avoid any attempt to
204205
* increment the iterator's key. */
205-
if (!$this->valid()) {
206+
if (! $this->valid()) {
206207
return;
207208
}
208209

@@ -236,6 +237,7 @@ private function resumeOrThrow(RuntimeException $exception)
236237
{
237238
if ($this->isResumableError($exception)) {
238239
$this->resume();
240+
239241
return;
240242
}
241243

src/Client.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,30 @@
1717

1818
namespace MongoDB;
1919

20+
use MongoDB\Driver\Exception\InvalidArgumentException as DriverInvalidArgumentException;
21+
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
2022
use MongoDB\Driver\Manager;
2123
use MongoDB\Driver\ReadConcern;
2224
use MongoDB\Driver\ReadPreference;
2325
use MongoDB\Driver\Session;
2426
use MongoDB\Driver\WriteConcern;
25-
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
26-
use MongoDB\Driver\Exception\InvalidArgumentException as DriverInvalidArgumentException;
2727
use MongoDB\Exception\InvalidArgumentException;
2828
use MongoDB\Exception\UnexpectedValueException;
2929
use MongoDB\Exception\UnsupportedException;
30+
use MongoDB\Model\BSONArray;
31+
use MongoDB\Model\BSONDocument;
3032
use MongoDB\Model\DatabaseInfoIterator;
3133
use MongoDB\Operation\DropDatabase;
3234
use MongoDB\Operation\ListDatabases;
3335
use MongoDB\Operation\Watch;
36+
use function is_array;
3437

3538
class Client
3639
{
3740
private static $defaultTypeMap = [
38-
'array' => \MongoDB\Model\BSONArray::class,
39-
'document' => \MongoDB\Model\BSONDocument::class,
40-
'root' => \MongoDB\Model\BSONDocument::class,
41+
'array' => BSONArray::class,
42+
'document' => BSONDocument::class,
43+
'root' => BSONDocument::class,
4144
];
4245
private static $wireVersionForReadConcern = 4;
4346
private static $wireVersionForWritableCommandWriteConcern = 5;
@@ -147,13 +150,13 @@ public function __toString()
147150
*/
148151
public function dropDatabase($databaseName, array $options = [])
149152
{
150-
if ( ! isset($options['typeMap'])) {
153+
if (! isset($options['typeMap'])) {
151154
$options['typeMap'] = $this->typeMap;
152155
}
153156

154157
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
155158

156-
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern)) {
159+
if (! isset($options['writeConcern']) && server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern)) {
157160
$options['writeConcern'] = $this->writeConcern;
158161
}
159162

@@ -269,7 +272,7 @@ public function selectDatabase($databaseName, array $options = [])
269272
* Start a new client session.
270273
*
271274
* @see http://php.net/manual/en/mongodb-driver-manager.startsession.php
272-
* @param array $options Session options
275+
* @param array $options Session options
273276
* @return Session
274277
*/
275278
public function startSession(array $options = [])
@@ -288,17 +291,17 @@ public function startSession(array $options = [])
288291
*/
289292
public function watch(array $pipeline = [], array $options = [])
290293
{
291-
if ( ! isset($options['readPreference'])) {
294+
if (! isset($options['readPreference'])) {
292295
$options['readPreference'] = $this->readPreference;
293296
}
294297

295298
$server = $this->manager->selectServer($options['readPreference']);
296299

297-
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
300+
if (! isset($options['readConcern']) && server_supports_feature($server, self::$wireVersionForReadConcern)) {
298301
$options['readConcern'] = $this->readConcern;
299302
}
300303

301-
if ( ! isset($options['typeMap'])) {
304+
if (! isset($options['typeMap'])) {
302305
$options['typeMap'] = $this->typeMap;
303306
}
304307

0 commit comments

Comments
 (0)