Skip to content

Commit 21781c9

Browse files
authored
PHPLIB-1238: Order internal methods after non-internal methods (#1162)
1 parent fdd7793 commit 21781c9

File tree

6 files changed

+144
-150
lines changed

6 files changed

+144
-150
lines changed

src/ChangeStream.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,6 @@ class ChangeStream implements Iterator
8787

8888
private ?DocumentCodec $codec;
8989

90-
/**
91-
* @internal
92-
*
93-
* @param ResumeCallable $resumeCallable
94-
*/
95-
public function __construct(ChangeStreamIterator $iterator, callable $resumeCallable, ?DocumentCodec $codec = null)
96-
{
97-
$this->iterator = $iterator;
98-
$this->resumeCallable = $resumeCallable;
99-
$this->codec = $codec;
100-
101-
if ($codec) {
102-
$this->iterator->getInnerIterator()->setTypeMap(['root' => 'bson']);
103-
}
104-
}
105-
10690
/**
10791
* @see https://php.net/iterator.current
10892
* @return array|object|null
@@ -200,6 +184,22 @@ public function valid()
200184
return $this->iterator->valid();
201185
}
202186

187+
/**
188+
* @internal
189+
*
190+
* @param ResumeCallable $resumeCallable
191+
*/
192+
public function __construct(ChangeStreamIterator $iterator, callable $resumeCallable, ?DocumentCodec $codec = null)
193+
{
194+
$this->iterator = $iterator;
195+
$this->resumeCallable = $resumeCallable;
196+
$this->codec = $codec;
197+
198+
if ($codec) {
199+
$this->iterator->getInnerIterator()->setTypeMap(['root' => 'bson']);
200+
}
201+
}
202+
203203
/**
204204
* Determines if an exception is a resumable error.
205205
*

src/MapReduceResult.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,6 @@ class MapReduceResult implements IteratorAggregate
5050

5151
private array $timing;
5252

53-
/**
54-
* @internal
55-
* @param callable $getIterator Callback that returns a Traversable for mapReduce results
56-
* @param stdClass $result Result document from the mapReduce command
57-
* @psalm-param MapReduceCallable $getIterator
58-
*/
59-
public function __construct(callable $getIterator, stdClass $result)
60-
{
61-
$this->getIterator = $getIterator;
62-
$this->executionTimeMS = isset($result->timeMillis) ? (integer) $result->timeMillis : 0;
63-
$this->counts = isset($result->counts) ? (array) $result->counts : [];
64-
$this->timing = isset($result->timing) ? (array) $result->timing : [];
65-
}
66-
6753
/**
6854
* Returns various count statistics from the mapReduce command.
6955
*
@@ -108,4 +94,18 @@ public function getTiming()
10894
{
10995
return $this->timing;
11096
}
97+
98+
/**
99+
* @internal
100+
* @param callable $getIterator Callback that returns a Traversable for mapReduce results
101+
* @param stdClass $result Result document from the mapReduce command
102+
* @psalm-param MapReduceCallable $getIterator
103+
*/
104+
public function __construct(callable $getIterator, stdClass $result)
105+
{
106+
$this->getIterator = $getIterator;
107+
$this->executionTimeMS = isset($result->timeMillis) ? (integer) $result->timeMillis : 0;
108+
$this->counts = isset($result->counts) ? (array) $result->counts : [];
109+
$this->timing = isset($result->timing) ? (array) $result->timing : [];
110+
}
111111
}

src/Model/BSONIterator.php

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,6 @@ class BSONIterator implements Iterator
5151

5252
private array $options;
5353

54-
/**
55-
* Constructs a BSON Iterator.
56-
*
57-
* Supported options:
58-
*
59-
* * typeMap (array): Type map for BSON deserialization.
60-
*
61-
* @internal
62-
* @see https://php.net/manual/en/function.mongodb.bson-tophp.php
63-
* @param string $data Concatenated, valid, BSON-encoded documents
64-
* @param array $options Iterator options
65-
* @throws InvalidArgumentException for parameter/option parsing errors
66-
*/
67-
public function __construct(string $data, array $options = [])
68-
{
69-
if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
70-
throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
71-
}
72-
73-
if (! isset($options['typeMap'])) {
74-
$options['typeMap'] = [];
75-
}
76-
77-
$this->buffer = $data;
78-
$this->bufferLength = strlen($data);
79-
$this->options = $options;
80-
}
81-
8254
/**
8355
* @see https://php.net/iterator.current
8456
* @return mixed
@@ -131,6 +103,34 @@ public function valid(): bool
131103
return $this->current !== null;
132104
}
133105

106+
/**
107+
* Constructs a BSON Iterator.
108+
*
109+
* Supported options:
110+
*
111+
* * typeMap (array): Type map for BSON deserialization.
112+
*
113+
* @internal
114+
* @see https://php.net/manual/en/function.mongodb.bson-tophp.php
115+
* @param string $data Concatenated, valid, BSON-encoded documents
116+
* @param array $options Iterator options
117+
* @throws InvalidArgumentException for parameter/option parsing errors
118+
*/
119+
public function __construct(string $data, array $options = [])
120+
{
121+
if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
122+
throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
123+
}
124+
125+
if (! isset($options['typeMap'])) {
126+
$options['typeMap'] = [];
127+
}
128+
129+
$this->buffer = $data;
130+
$this->bufferLength = strlen($data);
131+
$this->options = $options;
132+
}
133+
134134
private function advance(): void
135135
{
136136
if ($this->position === $this->bufferLength) {

src/Model/ChangeStreamIterator.php

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -68,71 +68,6 @@ class ChangeStreamIterator extends IteratorIterator implements CommandSubscriber
6868

6969
private Server $server;
7070

71-
/**
72-
* @internal
73-
* @param array|object|null $initialResumeToken
74-
* @psalm-param CursorInterface<int, TValue>&Iterator<int, TValue> $cursor
75-
*/
76-
public function __construct(CursorInterface $cursor, int $firstBatchSize, $initialResumeToken, ?object $postBatchResumeToken)
77-
{
78-
if (! $cursor instanceof Iterator) {
79-
throw InvalidArgumentException::invalidType(
80-
'$cursor',
81-
$cursor,
82-
CursorInterface::class . '&' . Iterator::class,
83-
);
84-
}
85-
86-
if (isset($initialResumeToken) && ! is_document($initialResumeToken)) {
87-
throw InvalidArgumentException::expectedDocumentType('$initialResumeToken', $initialResumeToken);
88-
}
89-
90-
parent::__construct($cursor);
91-
92-
$this->batchSize = $firstBatchSize;
93-
$this->isRewindNop = ($firstBatchSize === 0);
94-
$this->postBatchResumeToken = $postBatchResumeToken;
95-
$this->resumeToken = $initialResumeToken;
96-
$this->server = $cursor->getServer();
97-
}
98-
99-
/** @internal */
100-
final public function commandFailed(CommandFailedEvent $event): void
101-
{
102-
}
103-
104-
/** @internal */
105-
final public function commandStarted(CommandStartedEvent $event): void
106-
{
107-
if ($event->getCommandName() !== 'getMore') {
108-
return;
109-
}
110-
111-
$this->batchPosition = 0;
112-
$this->batchSize = 0;
113-
$this->postBatchResumeToken = null;
114-
}
115-
116-
/** @internal */
117-
final public function commandSucceeded(CommandSucceededEvent $event): void
118-
{
119-
if ($event->getCommandName() !== 'getMore') {
120-
return;
121-
}
122-
123-
$reply = $event->getReply();
124-
125-
if (! isset($reply->cursor->nextBatch) || ! is_array($reply->cursor->nextBatch)) {
126-
throw new UnexpectedValueException('getMore command did not return a "cursor.nextBatch" array');
127-
}
128-
129-
$this->batchSize = count($reply->cursor->nextBatch);
130-
131-
if (isset($reply->cursor->postBatchResumeToken) && is_object($reply->cursor->postBatchResumeToken)) {
132-
$this->postBatchResumeToken = $reply->cursor->postBatchResumeToken;
133-
}
134-
}
135-
13671
/**
13772
* @see https://php.net/iteratoriterator.current
13873
* @return array|object|null
@@ -239,6 +174,71 @@ public function valid(): bool
239174
return $this->isValid;
240175
}
241176

177+
/**
178+
* @internal
179+
* @param array|object|null $initialResumeToken
180+
* @psalm-param CursorInterface<int, TValue>&Iterator<int, TValue> $cursor
181+
*/
182+
public function __construct(CursorInterface $cursor, int $firstBatchSize, $initialResumeToken, ?object $postBatchResumeToken)
183+
{
184+
if (! $cursor instanceof Iterator) {
185+
throw InvalidArgumentException::invalidType(
186+
'$cursor',
187+
$cursor,
188+
CursorInterface::class . '&' . Iterator::class,
189+
);
190+
}
191+
192+
if (isset($initialResumeToken) && ! is_document($initialResumeToken)) {
193+
throw InvalidArgumentException::expectedDocumentType('$initialResumeToken', $initialResumeToken);
194+
}
195+
196+
parent::__construct($cursor);
197+
198+
$this->batchSize = $firstBatchSize;
199+
$this->isRewindNop = ($firstBatchSize === 0);
200+
$this->postBatchResumeToken = $postBatchResumeToken;
201+
$this->resumeToken = $initialResumeToken;
202+
$this->server = $cursor->getServer();
203+
}
204+
205+
/** @internal */
206+
final public function commandFailed(CommandFailedEvent $event): void
207+
{
208+
}
209+
210+
/** @internal */
211+
final public function commandStarted(CommandStartedEvent $event): void
212+
{
213+
if ($event->getCommandName() !== 'getMore') {
214+
return;
215+
}
216+
217+
$this->batchPosition = 0;
218+
$this->batchSize = 0;
219+
$this->postBatchResumeToken = null;
220+
}
221+
222+
/** @internal */
223+
final public function commandSucceeded(CommandSucceededEvent $event): void
224+
{
225+
if ($event->getCommandName() !== 'getMore') {
226+
return;
227+
}
228+
229+
$reply = $event->getReply();
230+
231+
if (! isset($reply->cursor->nextBatch) || ! is_array($reply->cursor->nextBatch)) {
232+
throw new UnexpectedValueException('getMore command did not return a "cursor.nextBatch" array');
233+
}
234+
235+
$this->batchSize = count($reply->cursor->nextBatch);
236+
237+
if (isset($reply->cursor->postBatchResumeToken) && is_object($reply->cursor->postBatchResumeToken)) {
238+
$this->postBatchResumeToken = $reply->cursor->postBatchResumeToken;
239+
}
240+
}
241+
242242
/**
243243
* Extracts the resume token (i.e. "_id" field) from a change document.
244244
*

src/Operation/Watch.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,23 @@ public function __construct(Manager $manager, ?string $databaseName, ?string $co
274274
$this->aggregate = $this->createAggregate();
275275
}
276276

277+
/**
278+
* Execute the operation.
279+
*
280+
* @see Executable::execute()
281+
* @return ChangeStream
282+
* @throws UnsupportedException if collation or read concern is used and unsupported
283+
* @throws RuntimeException for other driver errors (e.g. connection errors)
284+
*/
285+
public function execute(Server $server)
286+
{
287+
return new ChangeStream(
288+
$this->createChangeStreamIterator($server),
289+
fn ($resumeToken, $hasAdvanced): ChangeStreamIterator => $this->resume($resumeToken, $hasAdvanced),
290+
$this->codec,
291+
);
292+
}
293+
277294
/** @internal */
278295
final public function commandFailed(CommandFailedEvent $event): void
279296
{
@@ -317,23 +334,6 @@ final public function commandSucceeded(CommandSucceededEvent $event): void
317334
}
318335
}
319336

320-
/**
321-
* Execute the operation.
322-
*
323-
* @see Executable::execute()
324-
* @return ChangeStream
325-
* @throws UnsupportedException if collation or read concern is used and unsupported
326-
* @throws RuntimeException for other driver errors (e.g. connection errors)
327-
*/
328-
public function execute(Server $server)
329-
{
330-
return new ChangeStream(
331-
$this->createChangeStreamIterator($server),
332-
fn ($resumeToken, $hasAdvanced): ChangeStreamIterator => $this->resume($resumeToken, $hasAdvanced),
333-
$this->codec,
334-
);
335-
}
336-
337337
/**
338338
* Create the aggregate command for a change stream.
339339
*

tests/PedantryTest.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use function array_filter;
1212
use function array_map;
1313
use function realpath;
14+
use function str_contains;
1415
use function str_replace;
1516
use function strcasecmp;
1617
use function strlen;
@@ -37,17 +38,10 @@ public function testMethodsAreOrderedAlphabeticallyByVisibility($className): voi
3738
);
3839

3940
$getSortValue = function (ReflectionMethod $method) {
40-
if ($method->getModifiers() & ReflectionMethod::IS_PRIVATE) {
41-
return '2' . $method->getName();
42-
}
43-
44-
if ($method->getModifiers() & ReflectionMethod::IS_PROTECTED) {
45-
return '1' . $method->getName();
46-
}
41+
$prefix = $method->isPrivate() ? '2' : ($method->isProtected() ? '1' : '0');
42+
$prefix .= str_contains($method->getDocComment(), '@internal') ? '1' : '0';
4743

48-
if ($method->getModifiers() & ReflectionMethod::IS_PUBLIC) {
49-
return '0' . $method->getName();
50-
}
44+
return $prefix . $method->getName();
5145
};
5246

5347
$sortedMethods = $methods;

0 commit comments

Comments
 (0)