Skip to content

Commit

Permalink
Merge d72bd7f into 9bdd3e9
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jul 22, 2018
2 parents 9bdd3e9 + d72bd7f commit 3540948
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 11 deletions.
93 changes: 93 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

PRs and issues are linked, so you can find more about it. Thanks to [ChangelogLinker](https://github.com/Symplify/ChangelogLinker).

<!-- changelog-linker -->

## Unreleased

- [#79] Bump composer versions, Thanks to [@TomasVotruba]

## [v3.2.1] - 2018-05-12

- [#78] Decouple own config to allow importing the set, Thanks to [@TomasVotruba]

## [v3.2.0] - 2018-04-05

- [#77] bump to Symplify 4, Thanks to [@TomasVotruba]

## [v3.1.0] - 2017-10-19

- [#68] Replace example for Rule 2, Thanks to [@roukmoute]
- [#71] Small typo fixes, Thanks to [@noplanman]
- [#72] Cleanup and dependency updates, Thanks to [@TomasVotruba]

## [v3.0.0] - 2017-06-17

### Added

- [#64] [tests] use AbstractSniffRunner, no custom; bump to CodeSniffer 3.0, Thanks to [@TomasVotruba]
- [#58] Easy coding standard enabled + simplify and unite sniff structure + move rule [#8] , Thanks to [@TomasVotruba]
- [#51] Add minimal OneObjectOperatorPerLine config example, Thanks to [@UFOMelkor]
- [#50] Add few tests, Thanks to [@TomasVotruba]
- [#47] Make more sniff classes configurable, Thanks to [@TomasVotruba]
- [#46] Make rules configurable, Thanks to [@TomasVotruba]
- [#37] Ignore default keyword for switch/case, Thanks to [@roukmoute]

### Changed

- [#62] Bump to Codesniffer 3, Thanks to [@TomasVotruba]
- [#63] Changed Composer package type to `phpcodesniffer-standard`, Thanks to [@frenck]
- [#65] Polishing before release, Thanks to [@TomasVotruba]
- [#44] Cleanup README, bump to PHP 7.1, PHPUnit 6.0, drop unused code, Thanks to [@TomasVotruba]
- [#49] Bump to Code Sniffer 3-RC, Thanks to [@TomasVotruba]
- [#59] README: first changes, link to ruleset.xml settings, drop duplication, Thanks to [@TomasVotruba]

### Fixed

- [#56] Fixed README, Thanks to [@phux]
- [#55] Fixed error message for MaxNestingLevelSniff, Thanks to [@phux]
- [#53] Fix False positive (OneObjectOperatorPerLine) for property access within a class, Thanks to [@UFOMelkor]
- [#42] Class and function length: false positives, Thanks to [@UFOMelkor]

### Removed

- [#45] Drop rule 4, it is way too strict in practise, Thanks to [@TomasVotruba]

[#79]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/79
[#78]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/78
[#77]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/77
[#72]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/72
[#71]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/71
[#70]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/70
[#68]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/68
[#65]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/65
[#64]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/64
[#63]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/63
[#62]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/62
[#59]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/59
[#58]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/58
[#56]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/56
[#55]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/55
[#53]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/53
[#51]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/51
[#50]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/50
[#49]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/49
[#47]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/47
[#46]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/46
[#45]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/45
[#44]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/44
[#42]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/42
[#37]: https://github.com/object-calisthenics/phpcs-calisthenics-rules/pull/37
[@roukmoute]: https://github.com/roukmoute
[@phux]: https://github.com/phux
[@noplanman]: https://github.com/noplanman
[@frenck]: https://github.com/frenck
[@UFOMelkor]: https://github.com/UFOMelkor
[@TomasVotruba]: https://github.com/TomasVotruba
[@GaryJones]: https://github.com/GaryJones
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,16 @@ services:
ObjectCalisthenics\Sniffs\NamingConventions\NoSetterSniff: ~
```

#### :wrench: Configurable

```yml
# easy-coding-standard.yml
services:
ObjectCalisthenics\Sniffs\NamingConventions\NoSetterSniff:
allowedClasses:
- '*\DataObject'
```

---

### Not Implemented Rules - Too Strict, Vague or Annoying
Expand Down
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
"require": {
"php": "^7.1",
"squizlabs/php_codesniffer": "^3.3",
"nette/utils": "^2.4"
"nette/utils": "^2.5",
"slevomat/coding-standard": "^4.6"
},
"require-dev": {
"phpunit/phpunit": "^7.0",
"phpstan/phpstan": "^0.10.1",
"symplify/easy-coding-standard-tester": "^4.5",
"symplify/coding-standard": "^4.5",
"slevomat/coding-standard": "^4.6"
"tracy/tracy": "^2.5",
"symplify/changelog-linker": "^4.5"
},
"autoload": {
"psr-4": {
Expand All @@ -29,7 +31,8 @@
"complete-check": ["@phpstan", "@check-cs", "phpunit"],
"phpstan": "phpstan analyze src --level max",
"check-cs": "ecs check src tests",
"fix-cs": "ecs check src tests --fix"
"fix-cs": "ecs check src tests --fix",
"changelog": "changelog-linker dump-merges --in-categories"
},
"extra": {
"branch-alias": {
Expand Down
File renamed without changes.
54 changes: 46 additions & 8 deletions src/ObjectCalisthenics/Sniffs/NamingConventions/NoSetterSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace ObjectCalisthenics\Sniffs\NamingConventions;

use Nette\Utils\Strings;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\ClassHelper;

final class NoSetterSniff implements Sniff
{
Expand All @@ -16,7 +18,12 @@ final class NoSetterSniff implements Sniff
/**
* @var string
*/
private const SETTER_REGEX = '/^set[A-Z0-9]/';
private const SETTER_REGEX = '#^set[A-Z0-9]#';

/**
* @var string[]
*/
public $allowedClasses = [];

/**
* @return int[]
Expand All @@ -31,19 +38,50 @@ public function register(): array
*/
public function process(File $file, $position): void
{
$declarationName = $file->getDeclarationName($position);
if ($declarationName === null) {
$methodName = $file->getDeclarationName($position);
if ($methodName === null) {
return;
}

$className = $this->getClassName($file);
if ($this->shouldSkip($methodName, $className)) {
return;
}

if ($this->methodNameStartsWithSet($declarationName)) {
$file->addError(self::ERROR_MESSAGE, $position, self::class);
if (! (bool) Strings::match($methodName, self::SETTER_REGEX)) {
return;
}

$file->addError(self::ERROR_MESSAGE, $position, self::class);
}

private function methodNameStartsWithSet(string $methodName): bool
private function getClassName(File $file): string
{
return $methodName !== 'setUp'
&& preg_match(self::SETTER_REGEX, $methodName) === 1;
$classTokenPosition = $file->findNext(T_CLASS, 0);

// anonymous class
if (! is_integer($classTokenPosition)) {
return 'anonymous';
}

$className = ClassHelper::getFullyQualifiedName($file, $classTokenPosition);

return ltrim($className, '\\');
}

private function shouldSkip(string $methodName, string $className): bool
{
// not really a setter, but usually test "setup" method
if ($methodName === 'setUp') {
return true;
}

foreach ($this->allowedClasses as $allowedClass) {
if (fnmatch($allowedClass, $className, FNM_NOESCAPE)) {
return true;
}
}

return false;
}
}
23 changes: 23 additions & 0 deletions tests/Sniffs/NamingConventions/NoSetter/ConfiguredTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace ObjectCalisthenics\tests\Sniffs\NamingConventions;

use ObjectCalisthenics\Sniffs\NamingConventions\NoSetterSniff;
use Symplify\EasyCodingStandardTester\Testing\AbstractCheckerTestCase;

/**
* @see NoSetterSniff
*/
final class ConfiguredTest extends AbstractCheckerTestCase
{
public function testCorrectCases(): void
{
$this->doTestCorrectFile(__DIR__ . '/correct/correct2.php.inc');
$this->doTestCorrectFile(__DIR__ . '/correct/correct3.php.inc');
}

protected function provideConfig(): string
{
return __DIR__ . '/skipping-config.yml';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

class ValueDataObject
{
public function setThisIsAllowed()
{
}
}
10 changes: 10 additions & 0 deletions tests/Sniffs/NamingConventions/NoSetter/correct/correct3.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Allowed;

class SomeClass
{
public function setThisIsAllowed()
{
}
}
5 changes: 5 additions & 0 deletions tests/Sniffs/NamingConventions/NoSetter/skipping-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
ObjectCalisthenics\Sniffs\NamingConventions\NoSetterSniff:
allowedClasses:
- '*DataObject'
- 'Allowed\*'

0 comments on commit 3540948

Please sign in to comment.