Skip to content

New with parentheses #20

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 2 commits into from
Feb 6, 2018
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ exceptions/differences/extensions (:white_check_mark: are the implemented sniffs
- :white_check_mark: Omit phpDoc for parameters/returns with native types, unless adding description
- :white_check_mark: Don't use `@author`, `@since` and similar annotations that duplicate Git information
- :white_check_mark: Assignment in condition is not allowed
- :white_check_mark: Use parentheses when creating new instances that do not require arguments `$foo = new Foo()`

For full reference of enforcements, go through `lib/Doctrine/ruleset.xml` where each sniff is briefly described.

Expand Down
2 changes: 2 additions & 0 deletions lib/Doctrine/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowEqualOperators"/>
<!-- Require language constructs without parentheses -->
<rule ref="SlevomatCodingStandard.ControlStructures.LanguageConstructWithParentheses"/>
<!-- Require new instances with parentheses -->
<rule ref="SlevomatCodingStandard.ControlStructures.NewWithParentheses"/>
<!-- Forbid useless unreachable catch blocks -->
<rule ref="SlevomatCodingStandard.Exceptions.DeadCatch"/>
<!-- Require using Throwable instead of Exception -->
Expand Down
5 changes: 3 additions & 2 deletions tests/expected_report.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ FILE ERRORS WARNINGS
----------------------------------------------------------------------
tests/input/concatenation_spacing.php 24 0
tests/input/example-class.php 19 0
tests/input/new_with_parentheses.php 17 0
tests/input/not_spacing.php 7 0
tests/input/return_type_on_closures.php 21 0
tests/input/return_type_on_methods.php 17 0
tests/input/test-case.php 6 0
----------------------------------------------------------------------
A TOTAL OF 94 ERRORS AND 0 WARNINGS WERE FOUND IN 6 FILES
A TOTAL OF 111 ERRORS AND 0 WARNINGS WERE FOUND IN 7 FILES
----------------------------------------------------------------------
PHPCBF CAN FIX 83 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
PHPCBF CAN FIX 100 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


28 changes: 28 additions & 0 deletions tests/fixed/new_with_parentheses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

$foo = new DateTimeImmutable();

$barClassName = 'Bar';
$bar = new $barClassName();

$classNamesInArray = ['Baz'];
$foo = new $classNamesInArray[0]();

$classNamesInObject = new stdClass();
$classNamesInObject->foo = 'Foo';
$foo = new $classNamesInObject->foo();

$whitespaceBetweenClassNameAndParentheses = new stdClass() ;

$x = [
new stdClass(),
];

$y = [new stdClass()];

$z = new stdClass() ? new stdClass() : new stdClass();

$q = $q ?: new stdClass();
$e = $e ?? new stdClass();
28 changes: 28 additions & 0 deletions tests/input/new_with_parentheses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

$foo = new \DateTimeImmutable;

$barClassName = 'Bar';
$bar = new $barClassName;

$classNamesInArray = ['Baz'];
$foo = new $classNamesInArray[0];

$classNamesInObject = new stdClass();
$classNamesInObject->foo = 'Foo';
$foo = new $classNamesInObject->foo;

$whitespaceBetweenClassNameAndParentheses = new stdClass ;

$x = [
new stdClass,
];

$y = [new stdClass];

$z = new stdClass ? new stdClass : new stdClass;

$q = $q ?: new stdClass;
$e = $e ?? new stdClass;