Skip to content

Improve coding standard #1136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 9 additions & 31 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
<!-- Exclude sniffs that cause a huge diff - enable separately -->
<!-- ********************************************************* -->
<exclude name="SlevomatCodingStandard.Commenting.DocCommentSpacing.IncorrectAnnotationsGroup" />
<exclude name="Squiz.Strings.DoubleQuoteUsage" />


<!-- ********************* -->
Expand All @@ -78,9 +77,13 @@
</rule>


<!-- ***************************************************** -->
<!-- **************************************** -->
<!-- Enable rules not enforced by Doctrine CS -->
<!-- **************************************** -->

<!-- Require arrow functions where possible -->
<rule ref="SlevomatCodingStandard.Functions.RequireArrowFunction"/>
<!-- Forbid fully qualified names even for colliding names -->
<!-- ***************************************************** -->
<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly">
<properties>
<property name="allowFallbackGlobalConstants" value="false"/>
Expand All @@ -96,41 +99,16 @@
</rule>


<!-- **************************************************************************** -->
<!-- Exclude BC breaking type hints for parameters, properties, and return values -->
<!-- **************************************************************************** -->
<!-- ****************************************************** -->
<!-- Don't require annotations to specify traversable types -->
<!-- ****************************************************** -->
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint">
<properties>
<!-- Requires PHP 8.0 -->
<property name="enableMixedTypeHint" value="false" />
<!-- Requires PHP 8.0 -->
<property name="enableUnionTypeHint" value="false" />
</properties>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why were these removed for parameters, properties, and return types? Is it irrelevant since we're forcing a lower php_version above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct. When I added the config we didn't use php_version yet, so I added these as a safeguard against for when someone runs phpcs on a newer version (I typically run PHP 8.2 these days and only use an older version if necessary for specific testing). This came to bite me when phpcs didn't enforce native property types because the property was still set to false in this block.

With php_version this becomes irrelevant and the version set there takes precedence over the PHP version being used to run phpcs, so the auto-detection for those features works again and does not enable mixed types or union types until we update php_version to 80000.


<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification" />
</rule>

<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint">
<properties>
<!-- Requires PHP 8.0 -->
<property name="enableMixedTypeHint" value="false" />
<!-- Requires PHP 8.0 -->
<property name="enableUnionTypeHint" value="false" />
</properties>

<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingTraversableTypeHintSpecification" />
</rule>

<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint">
<properties>
<!-- Requires PHP 8.0 -->
<property name="enableStaticTypeHint" value="false" />
<!-- Requires PHP 8.0 -->
<property name="enableMixedTypeHint" value="false" />
<!-- Requires PHP 8.0 -->
<property name="enableUnionTypeHint" value="false" />
</properties>

<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification" />
</rule>

Expand Down
8 changes: 4 additions & 4 deletions src/GridFS/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public function delete($id)
*/
public function downloadToStream($id, $destination)
{
if (! is_resource($destination) || get_resource_type($destination) != "stream") {
if (! is_resource($destination) || get_resource_type($destination) != 'stream') {
throw InvalidArgumentException::invalidType('$destination', $destination, 'resource');
}

Expand Down Expand Up @@ -272,7 +272,7 @@ public function downloadToStream($id, $destination)
*/
public function downloadToStreamByName(string $filename, $destination, array $options = [])
{
if (! is_resource($destination) || get_resource_type($destination) != "stream") {
if (! is_resource($destination) || get_resource_type($destination) != 'stream') {
throw InvalidArgumentException::invalidType('$destination', $destination, 'resource');
}

Expand Down Expand Up @@ -616,7 +616,7 @@ public function rename($id, string $newFilename)
*/
public function uploadFromStream(string $filename, $source, array $options = [])
{
if (! is_resource($source) || get_resource_type($source) != "stream") {
if (! is_resource($source) || get_resource_type($source) != 'stream') {
throw InvalidArgumentException::invalidType('$source', $source, 'resource');
}

Expand Down Expand Up @@ -685,7 +685,7 @@ private function getFilesNamespace(): string
*/
private function getRawFileDocumentForStream($stream): object
{
if (! is_resource($stream) || get_resource_type($stream) != "stream") {
if (! is_resource($stream) || get_resource_type($stream) != 'stream') {
throw InvalidArgumentException::invalidType('$stream', $stream, 'resource');
}

Expand Down
2 changes: 1 addition & 1 deletion tests/DocumentationExamplesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1403,7 +1403,7 @@ private function commitWithRetry3(\MongoDB\Driver\Session $session): void
private function updateEmployeeInfo3(\MongoDB\Client $client, \MongoDB\Driver\Session $session): void
{
$session->startTransaction([
'readConcern' => new \MongoDB\Driver\ReadConcern("snapshot"),
'readConcern' => new \MongoDB\Driver\ReadConcern('snapshot'),
'readPrefernece' => new \MongoDB\Driver\ReadPreference(\MongoDB\Driver\ReadPreference::PRIMARY),
'writeConcern' => new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY),
]);
Expand Down
2 changes: 1 addition & 1 deletion tests/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public static function isCryptSharedLibAvailable(): bool
/** @see https://www.mongodb.com/docs/manual/core/queryable-encryption/reference/mongocryptd/ */
public static function isMongocryptdAvailable(): bool
{
$paths = explode(PATH_SEPARATOR, getenv("PATH"));
$paths = explode(PATH_SEPARATOR, getenv('PATH'));

foreach ($paths as $path) {
if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) {
Expand Down
6 changes: 3 additions & 3 deletions tests/GridFS/BucketFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function testDeleteStillRemovesChunksIfFileDoesNotExist($input, $expected

public function testDownloadingFileWithMissingChunk(): void
{
$id = $this->bucket->uploadFromStream("filename", $this->createStream("foobar"));
$id = $this->bucket->uploadFromStream('filename', $this->createStream('foobar'));

$this->chunksCollection->deleteOne(['files_id' => $id, 'n' => 0]);

Expand All @@ -151,7 +151,7 @@ public function testDownloadingFileWithMissingChunk(): void

public function testDownloadingFileWithUnexpectedChunkIndex(): void
{
$id = $this->bucket->uploadFromStream("filename", $this->createStream("foobar"));
$id = $this->bucket->uploadFromStream('filename', $this->createStream('foobar'));

$this->chunksCollection->updateOne(
['files_id' => $id, 'n' => 0],
Expand All @@ -165,7 +165,7 @@ public function testDownloadingFileWithUnexpectedChunkIndex(): void

public function testDownloadingFileWithUnexpectedChunkSize(): void
{
$id = $this->bucket->uploadFromStream("filename", $this->createStream("foobar"));
$id = $this->bucket->uploadFromStream('filename', $this->createStream('foobar'));

$this->chunksCollection->updateOne(
['files_id' => $id, 'n' => 0],
Expand Down
6 changes: 2 additions & 4 deletions tests/PedantryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ public function testMethodsAreOrderedAlphabeticallyByVisibility($className): voi

$methods = array_filter(
$methods,
function (ReflectionMethod $method) use ($class) {
return $method->getDeclaringClass() == $class // Exclude inherited methods
&& $method->getFileName() === $class->getFileName(); // Exclude methods inherited from traits
},
fn (ReflectionMethod $method) => $method->getDeclaringClass() == $class // Exclude inherited methods
&& $method->getFileName() === $class->getFileName(), // Exclude methods inherited from traits
);

$getSortValue = function (ReflectionMethod $method) {
Expand Down
2 changes: 1 addition & 1 deletion tools/detect-extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function grepIniFile(string $filename, string $extension): int
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
$zts = PHP_ZTS ? 'Thread Safe (TS)' : 'Non Thread Safe (NTS)';
$arch = PHP_INT_SIZE === 8 ? 'x64' : 'x86';
$dll = sprintf("%d.%d %s %s", PHP_MAJOR_VERSION, PHP_MINOR_VERSION, $zts, $arch);
$dll = sprintf('%d.%d %s %s', PHP_MAJOR_VERSION, PHP_MINOR_VERSION, $zts, $arch);

printf("You likely need to download a Windows DLL for: %s\n", $dll);
printf("Windows DLLs should be available from: https://pecl.php.net/package/%s\n", $extension);
Expand Down