Skip to content

Commit 36eceae

Browse files
authored
Merge pull request #6 from SpacePossum/1_0_update_CS
Housekeeping
2 parents 8334855 + e5f14b2 commit 36eceae

File tree

7 files changed

+81
-62
lines changed

7 files changed

+81
-62
lines changed

.php_cs.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This file is part of the GeckoPackages.
1717
This source file is subject to the MIT license that is bundled
1818
with this source code in the file LICENSE.
1919
EOF;
20+
2021
return (new PhpCsFixer\Config('GeckoPackagesCS'))
2122
->setRiskyAllowed(true)
2223
->setRules([
@@ -25,15 +26,18 @@ return (new PhpCsFixer\Config('GeckoPackagesCS'))
2526
'array_syntax' => ['syntax' => 'short'],
2627
'class_definition' => ['singleItemSingleLine' => true, 'multiLineExtendsEachSingleLine' => true],
2728
'combine_consecutive_unsets' => true,
29+
'declare_strict_types' => true,
2830
'dir_constant' => true,
2931
'ereg_to_preg' => true,
3032
'header_comment' => ['header' => $header],
3133
'heredoc_to_nowdoc' => true,
3234
'modernize_types_casting' => true,
35+
'native_function_invocation' => true,
3336
'no_extra_consecutive_blank_lines' => ['break', 'continue', 'curly_brace_block', 'extra', 'parenthesis_brace_block', 'return', 'square_brace_block', 'throw', 'use', 'useTrait'],
3437
'no_multiline_whitespace_before_semicolons' => true,
3538
'no_php4_constructor' => true,
3639
'no_short_echo_tag' => true,
40+
'no_unreachable_default_argument_value' => true,
3741
'no_useless_else' => true,
3842
'no_useless_return' => true,
3943
'ordered_class_elements' => true,
@@ -48,6 +52,7 @@ return (new PhpCsFixer\Config('GeckoPackagesCS'))
4852
'semicolon_after_instruction' => true,
4953
'strict_comparison' => true,
5054
'strict_param' => true,
55+
'ternary_to_null_coalescing' => true,
5156
// remove import from set
5257
'phpdoc_summary' => false,
5358
])

.travis.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ matrix:
99
fast_finish: true
1010
include:
1111
- php: 7.0
12-
env: COMPOSER_FLAGS="--prefer-stable"
12+
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
1313
- php: 7.1
14+
env: COMPOSER_FLAGS="--prefer-stable"
15+
- php: 7.2
1416
env: COMPOSER_FLAGS="--dev"
1517
- php: nightly
1618
env: COMPOSER_FLAGS="--dev"
@@ -26,10 +28,13 @@ before_install:
2628
- echo extension=mongodb.so >> $INI_FILE;
2729

2830
install:
29-
- composer update $COMPOSER_FLAGS --no-interaction -v
30-
- if [[ $TRAVIS_PHP_VERSION = 7.0 ]]; then curl -L https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.0.0/php-cs-fixer.phar -o php-cs-fixer.phar; fi
31+
- travis_retry composer update $COMPOSER_FLAGS --no-interaction -v
32+
- composer info -D | sort
33+
- php --ri mongodb|grep version
34+
- if [[ $TRAVIS_PHP_VERSION = 7.1 ]]; then curl -L https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.3.1/php-cs-fixer.phar -o php-cs-fixer.phar; fi
3135

3236
script:
33-
- phpunit --verbose
3437
- phpenv config-rm xdebug.ini || return 0
35-
- if [[ $TRAVIS_PHP_VERSION = 7.0 ]]; then php php-cs-fixer.phar --dry-run --diff -vvv fix; fi
38+
- ./vendor/bin/phpunit --verbose
39+
- if [[ $TRAVIS_PHP_VERSION = 7.1 ]]; then php php-cs-fixer.phar --dry-run --diff -vvv fix; fi
40+
- if [[ $TRAVIS_PHP_VERSION = 7.1 ]]; then files_with_trailing_spaces=`find . -type f -not -path "./.git/*" -not -path "./vendor/*" -not -path "./tests/assets/*" -exec egrep -nH " $" {} \;` && [[ $files_with_trailing_spaces ]] && echo $files_with_trailing_spaces || echo "No trailing spaces detected."; fi

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# BSON file iterator
44

5-
Iterator for [BSON files](https://en.wikipedia.org/wiki/BSON), for example as produced by [mongodump](https://docs.mongodb.com/manual/reference/program/mongodump/).
5+
Iterator for [BSON](https://en.wikipedia.org/wiki/BSON) files, for example as produced by [mongodump](https://docs.mongodb.com/manual/reference/program/mongodump/).
66
The iterator can return the values it reads from the file by converting those into:
77
- *string*s with JSON encoded data
88
- associative *array*s
@@ -78,12 +78,15 @@ The project is released under the MIT license, see the LICENSE file.
7878

7979
### Contributions
8080

81-
Contributions are welcome!
81+
Contributions are welcome!<br/>
82+
Visit us on [github :octocat:](https://github.com/GeckoPackages/GeckoBSONFileIterator)
8283

8384
### Semantic Versioning
8485

8586
This project follows [Semantic Versioning](http://semver.org/).
8687

8788
<sub>Kindly note:
88-
We do not keep a backwards compatible promise on the tests and tooling (such as document generation) of the project itself
89-
nor the content and/or format of exception messages.</sub>
89+
We do not keep a backwards compatible promise on code annotated with `@internal`, the tests and tooling (such as document generation) of the project itself
90+
nor the content and/or format of exception/error messages.</sub>
91+
92+
This project is maintained on [github :octocat:](https://github.com/GeckoPackages/GeckoBSONFileIterator)

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"mongodb/mongodb": "^1.0.0"
1111
},
1212
"require-dev": {
13-
"gecko-packages/gecko-php-unit": "^2.0"
13+
"gecko-packages/gecko-php-unit": "^3.0",
14+
"phpunit/phpunit": "^6.0"
1415
},
1516
"autoload": {
1617
"psr-4": {

src/Bson/BsonFileIterator.php

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function __construct(
7878

7979
public function __destruct()
8080
{
81-
@fclose($this->fileHandle); // best effort
81+
@\fclose($this->fileHandle); // best effort
8282
}
8383

8484
/**
@@ -112,26 +112,26 @@ public function key()
112112
*/
113113
public function next()
114114
{
115-
$this->content = fread($this->fileHandle, 4);
116-
if (feof($this->fileHandle)) {
115+
$this->content = \fread($this->fileHandle, 4);
116+
if (\feof($this->fileHandle)) {
117117
$this->content = false;
118118

119119
return;
120120
}
121121

122-
fseek($this->fileHandle, -4, SEEK_CUR);
123-
$this->content = unpack('V', $this->content);
124-
$length = array_shift($this->content);
122+
\fseek($this->fileHandle, -4, SEEK_CUR);
123+
$this->content = \unpack('V', $this->content);
124+
$length = \array_shift($this->content);
125125

126126
if ($length > $this->maxUnpackSize) {
127-
throw new \UnexpectedValueException(sprintf(
127+
throw new \UnexpectedValueException(\sprintf(
128128
'Invalid data at item #%d, size %d exceeds max. unpack size %d.',
129129
1 + $this->key, $length, $this->maxUnpackSize
130130
));
131131
}
132132

133133
$decoder = $this->decoder;
134-
$this->content = $decoder($this->key, \MongoDB\BSON\toJSON(fread($this->fileHandle, $length)));
134+
$this->content = $decoder($this->key, \MongoDB\BSON\toJSON(\fread($this->fileHandle, $length)));
135135

136136
++$this->key;
137137
}
@@ -141,7 +141,7 @@ public function next()
141141
*/
142142
public function rewind()
143143
{
144-
fseek($this->fileHandle, 0);
144+
\fseek($this->fileHandle, 0);
145145
$this->next(); // implicit sets `$this->content`
146146
$this->key = 0;
147147
}
@@ -158,25 +158,25 @@ private function resolveFileHandle($file)
158158
{
159159
if ($file instanceof \SplFileInfo) {
160160
$file = $file->getPathname();
161-
} elseif (!is_string($file)) {
162-
throw new \InvalidArgumentException(sprintf(
161+
} elseif (!\is_string($file)) {
162+
throw new \InvalidArgumentException(\sprintf(
163163
'%s is not a file.',
164-
is_object($file) ? get_class($file) : gettype($file).(is_resource($file) ? '' : '#"'.$file.'"')
164+
\is_object($file) ? \get_class($file) : \gettype($file).(\is_resource($file) ? '' : '#"'.$file.'"')
165165
));
166166
}
167167

168-
if (!is_file($file)) {
169-
throw new \InvalidArgumentException(sprintf('%s is not a file.', is_dir($file) ? 'directory#"'.$file.'"' : '"'.$file.'"'));
168+
if (!\is_file($file)) {
169+
throw new \InvalidArgumentException(\sprintf('%s is not a file.', \is_dir($file) ? 'directory#"'.$file.'"' : '"'.$file.'"'));
170170
}
171171

172-
if (!is_readable($file)) {
173-
throw new \InvalidArgumentException(sprintf('file "%s" is not readable.', $file));
172+
if (!\is_readable($file)) {
173+
throw new \InvalidArgumentException(\sprintf('file "%s" is not readable.', $file));
174174
}
175175

176-
$fileHandle = @fopen($file, 'rb');
176+
$fileHandle = @\fopen($file, 'rb');
177177
if (false === $fileHandle) {
178-
$error = error_get_last();
179-
throw new \RuntimeException(sprintf(
178+
$error = \error_get_last();
179+
throw new \RuntimeException(\sprintf(
180180
'Failed to open file "%s" for reading.%s',
181181
$file, null === $error ? '' : ' '.$error['message']
182182
));
@@ -203,23 +203,23 @@ private function resolveDecoder(int $constructType, int $jsonDecodeMaxDepth, int
203203

204204
break;
205205
default:
206-
throw new \InvalidArgumentException(sprintf(
206+
throw new \InvalidArgumentException(\sprintf(
207207
'Construct type must be any of integers "%s" got "%d".',
208-
implode(', ', [self::CONSTRUCT_JSON, self::CONSTRUCT_ARRAY, self::CONSTRUCT_STD]), $constructType
208+
\implode(', ', [self::CONSTRUCT_JSON, self::CONSTRUCT_ARRAY, self::CONSTRUCT_STD]), $constructType
209209
));
210210
}
211211

212212
if ($jsonDecodeMaxDepth < 1) {
213-
throw new \InvalidArgumentException(sprintf(
213+
throw new \InvalidArgumentException(\sprintf(
214214
'Expected integer > 0 for JSON decode max depth, got "%s".',
215-
is_object($jsonDecodeMaxDepth) ? get_class($jsonDecodeMaxDepth) : gettype($jsonDecodeMaxDepth).(is_resource($jsonDecodeMaxDepth) ? '' : '#'.$jsonDecodeMaxDepth)
215+
\is_object($jsonDecodeMaxDepth) ? \get_class($jsonDecodeMaxDepth) : \gettype($jsonDecodeMaxDepth).(\is_resource($jsonDecodeMaxDepth) ? '' : '#'.$jsonDecodeMaxDepth)
216216
));
217217
}
218218

219219
$this->decoder = function ($key, $content) use ($assoc, $jsonDecodeMaxDepth, $jsonDecodeOptions) {
220-
$content = @json_decode($content, $assoc, $jsonDecodeMaxDepth, $jsonDecodeOptions);
220+
$content = @\json_decode($content, $assoc, $jsonDecodeMaxDepth, $jsonDecodeOptions);
221221
if (null === $content) {
222-
throw new \UnexpectedValueException(sprintf('Invalid JSON "%s" at item #%d.', json_last_error_msg(), 1 + $key));
222+
throw new \UnexpectedValueException(\sprintf('Invalid JSON "%s" at item #%d.', \json_last_error_msg(), 1 + $key));
223223
}
224224

225225
return $content;
@@ -235,12 +235,12 @@ private function resolveDecoder(int $constructType, int $jsonDecodeMaxDepth, int
235235
private function resolveMaxUnpackSize(int $maxUnpackSize, string $file)
236236
{
237237
if ($maxUnpackSize <= 0) {
238-
throw new \InvalidArgumentException(sprintf(
238+
throw new \InvalidArgumentException(\sprintf(
239239
'Expected integer > 0 for max. unpack size, got "%s".',
240-
is_object($maxUnpackSize) ? get_class($maxUnpackSize) : gettype($maxUnpackSize).(is_resource($maxUnpackSize) ? '' : '#'.$maxUnpackSize)
240+
\is_object($maxUnpackSize) ? \get_class($maxUnpackSize) : \gettype($maxUnpackSize).(\is_resource($maxUnpackSize) ? '' : '#'.$maxUnpackSize)
241241
));
242242
}
243243

244-
$this->maxUnpackSize = min($maxUnpackSize, filesize($file));
244+
$this->maxUnpackSize = \min($maxUnpackSize, \filesize($file));
245245
}
246246
}

tests/Bson/BsonFileIteratorTest.php

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/*
46
* This file is part of the GeckoPackages.
57
*
@@ -11,6 +13,7 @@
1113

1214
use GeckoPackages\Bson\BsonFileIterator;
1315
use GeckoPackages\PHPUnit\Asserts\RangeAssertTrait;
16+
use PHPUnit\Framework\TestCase;
1417

1518
/**
1619
* @requires PHPUnit 5.2
@@ -19,7 +22,7 @@
1922
*
2023
* @author SpacePossum
2124
*/
22-
final class BsonFileIteratorTest extends \PHPUnit_Framework_TestCase
25+
final class BsonFileIteratorTest extends TestCase
2326
{
2427
use RangeAssertTrait;
2528

@@ -37,25 +40,25 @@ public function testIterator($file, $constructType, array $expected)
3740
foreach ($iterator as $index => $item) {
3841
$this->assertUnsignedInt($index);
3942

40-
if (is_string($item) && BsonFileIterator::CONSTRUCT_JSON === $constructType) {
41-
$item = json_decode($item, true);
43+
if (\is_string($item) && BsonFileIterator::CONSTRUCT_JSON === $constructType) {
44+
$item = \json_decode($item, true);
4245
foreach ($item as $p => $v) {
43-
if (is_float($v)) {
44-
$item[$p] = round($v, 5);
46+
if (\is_float($v)) {
47+
$item[$p] = \round($v, 5);
4548
}
4649
}
4750

48-
$item = json_encode($item, JSON_PRETTY_PRINT);
49-
} elseif (is_array($item) && BsonFileIterator::CONSTRUCT_ARRAY === $constructType) {
51+
$item = \json_encode($item, JSON_PRETTY_PRINT);
52+
} elseif (\is_array($item) && BsonFileIterator::CONSTRUCT_ARRAY === $constructType) {
5053
foreach ($item as $p => $v) {
51-
if (is_float($v)) {
52-
$item[$p] = round($v, 5);
54+
if (\is_float($v)) {
55+
$item[$p] = \round($v, 5);
5356
}
5457
}
5558
} elseif ($item instanceof \stdClass && BsonFileIterator::CONSTRUCT_STD === $constructType) {
5659
foreach ($item as $p => $v) {
57-
if (is_float($v)) {
58-
$item->$p = round($v, 5);
60+
if (\is_float($v)) {
61+
$item->$p = \round($v, 5);
5962
}
6063
}
6164
}
@@ -64,9 +67,9 @@ public function testIterator($file, $constructType, array $expected)
6467
}
6568

6669
if (BsonFileIterator::CONSTRUCT_STD === $constructType) {
67-
$this->assertCount(count($expected), $expected);
70+
$this->assertCount(\count($expected), $expected);
6871
$this->assertContainsOnlyInstancesOf(\stdClass::class, $expected);
69-
for ($i = 0, $count = count($expected); $i < $count; ++$i) {
72+
for ($i = 0, $count = \count($expected); $i < $count; ++$i) {
7073
$expect = $expected[$i];
7174
$actual = $unWinded[$i];
7275
foreach ($expect as $key => $value) {
@@ -90,7 +93,7 @@ public function testIterator($file, $constructType, array $expected)
9093
$iterator->__destruct();
9194
}
9295

93-
public function provideIteratorCases()
96+
public function provideIteratorCases(): array
9497
{
9598
$assertDir = $this->getAssetDir();
9699

@@ -117,9 +120,9 @@ public function provideIteratorCases()
117120
'e' => [],
118121
'f' => 'abc',
119122
'g' => PHP_INT_MAX,
120-
'h' => round(-1.11111111, 5),
123+
'h' => \round(-1.11111111, 5),
121124
'i' => PHP_INT_MIN,
122-
'j' => round(1.11111111, 5),
125+
'j' => \round(1.11111111, 5),
123126
'k' => 0,
124127
'l' => [
125128
1, 2, 3,
@@ -134,9 +137,9 @@ public function provideIteratorCases()
134137
'e' => [],
135138
'f' => 'abc',
136139
'g' => PHP_INT_MAX,
137-
'h' => round(-1.11111111, 5),
140+
'h' => \round(-1.11111111, 5),
138141
'i' => PHP_INT_MIN,
139-
'j' => round(1.11111111, 5),
142+
'j' => \round(1.11111111, 5),
140143
'k' => 0,
141144
'l' => [
142145
1, 2, 3,
@@ -152,7 +155,7 @@ public function provideIteratorCases()
152155
$case = $cases[$index];
153156
$expected = [];
154157
foreach ($case[2] as $j => $expect) {
155-
$expected[$j] = json_encode($expect, JSON_PRETTY_PRINT);
158+
$expected[$j] = \json_encode($expect, JSON_PRETTY_PRINT);
156159
}
157160

158161
$cases['json test.bson'] = [
@@ -183,7 +186,7 @@ public function provideIteratorCases()
183186
public function testIteratorSplFile()
184187
{
185188
$cases = $this->provideIteratorCases();
186-
$case = reset($cases);
189+
$case = \reset($cases);
187190
$this->testIterator(new \SplFileInfo($case[0]), $case[1], $case[2]);
188191
}
189192

@@ -275,7 +278,7 @@ public function testIteratorMaxDepthReached($constructType)
275278
$this->expectExceptionMessageRegExp('#^Invalid JSON \"Maximum stack depth exceeded\" at item \#1.$#');
276279

277280
$cases = $this->provideIteratorCases();
278-
$case = reset($cases);
281+
$case = \reset($cases);
279282
$iterator = new BsonFileIterator(
280283
$case[0],
281284
$constructType,
@@ -286,13 +289,13 @@ public function testIteratorMaxDepthReached($constructType)
286289
$this->assertCount(0, $iterator); // not a real assert, but causes the iterator to real all
287290
}
288291

289-
public function provideIteratorMaxDepthReached()
292+
public function provideIteratorMaxDepthReached(): array
290293
{
291294
return [[BsonFileIterator::CONSTRUCT_ARRAY], [BsonFileIterator::CONSTRUCT_STD]];
292295
}
293296

294-
private function getAssetDir()
297+
private function getAssetDir(): string
295298
{
296-
return realpath(__DIR__.'/../assets').'/';
299+
return \realpath(__DIR__.'/../assets').'/';
297300
}
298301
}

tests/autoload.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/*
46
* This file is part of the GeckoPackages.
57
*

0 commit comments

Comments
 (0)