object-calisthenics / phpcs-calisthenics-rules Goto Github PK
View Code? Open in Web Editor NEW[DEPRECATED] Object Calisthenics rules for PHP_CodeSniffer
License: MIT License
[DEPRECATED] Object Calisthenics rules for PHP_CodeSniffer
License: MIT License
The original text says:
Rule 9: No getters/setters/properties
From what I understand PSR-2 isn't really part of calisthenics principles. And some projects aren't PSR-2. And will never be because reasons. :)
With current version of CodeSniffer it's much easier to use multiple rulesets (comma separated list of them), than throw something out (need to create custom ruleset, import another ruleset, then exclude parts you don't need/want).
Hello. After installing using composer:
PHP Fatal error: Uncaught Error: Class 'Nette\Utils\Strings' not found
Composer output:
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 2 installs, 5 updates, 0 removals
- Updating doctrine/dbal (v2.6.2 => v2.6.3): Loading from cache
- Updating phpdocumentor/reflection-docblock (4.1.1 => 4.2.0): Downloading (100%)
- Updating phpspec/prophecy (v1.7.2 => 1.7.3): Downloading (100%)
- Updating phpunit/php-file-iterator (1.4.2 => 1.4.5): Downloading (100%)
- Updating phpunit/php-token-stream (2.0.1 => 2.0.2): Downloading (100%)
- Installing nette/utils (v2.4.8): Downloading (100%)
- Installing object-calisthenics/phpcs-calisthenics-rules (v3.1.0): Downloading (100%)
nette/utils suggests installing ext-intl (for script transliteration in Strings::webalize() and toAscii())
nette/utils suggests installing ext-gd (to use Image)
Writing lock file
Generating autoload files
Hello!
I've just seen when a method contains a switch/case statement, for the interpreter there is two indentations level.
I understand the idea, but in fact there is only one level, no ?
Could we improve this rule?
See you ;)
I was trying to make some research about Object Calisthenics
So about this sniff I was wondering.
__toString()
?This one shall not be reported, as it is part of fluent interface, that is described in map of methods.
/**
* @return TrainingTerm[]
*/
public function fetchPast(): array
{
return $this->entityRepository->createQueryBuilder('tt')
->where('tt.endDateTime < CURRENT_DATE()')
->getQuery()
->getResult();
}
public function addLinkBetweenOauthClientAndUserFromIds($oauthClientId, $userId)
{
try {
$oauthClient = $this->oauthClientRepository->get($oauthClientId);
if ($oauthClient === null) {
return false;
}
$user = $this->userRepository->get($userId);
if ($user === null) {
return false;
}
return $this->addLinkBetweenOauthClientAndUser($oauthClient, $user);
} catch (Exception $exception) {
$this->logger->error(
'Can\'t link from their ids oauth client: ' . $oauthClient->getId() . ' and user: ' . $user->getId()
. '. Error: ' .$exception->getMessage()
);
return false;
}
}
With a code like that, i mean code in a try catch, only one level seems too restrictive no ?
So maybe an exception of two level when the first or the second is a try catch ?
How to do to exit a loop properly when you can only have one level ?
I can't found a good solution.
$element = ['A1', 'A2', 'A3', 'A4', 'A5'];
function exitWhenA2Found($value, $element)
{
if ($value === 'A2') {
$element->seek($element->count() - 1);
}
}
function test($element)
{
$iterator = new ArrayIterator($element);
foreach ($iterator as $value) {
exitWhenA2Found($value, $iterator);
echo $value . "\n";
}
}
test($element);
// VS
function testClassic($element)
{
$iterator = new ArrayIterator($element);
foreach ($iterator as $value) {
if ($value === 'A2') {
break;
}
echo $value . "\n";
}
}
This solution is the better one i found.
But i don't think its a nice solution because it's not natural at all, and you need to go into exitWhenA2Found to know it gonna emulate a break (and not really like a true break because all code after the call in the current iteration is executed)
PHP switch statement is being detected as two levels of logic nesting. This seems incorrect, unless the intent is to discourage switch altogether.
On this code:
/**
* Create object and set parameters from passed.
*
* @param array $args
*/
public function __construct( $args ) { // line 19 here
$this->type = $args['type'];
$this->timeout = $args['timeout'];
}
I get following errors:
19 | ERROR | Unable to retrieve data type of property "$args"
| | (ObjectCalisthenics.CodeAnalysis.ArrayPropertyPerClassLimit.InvalidPropertyType)
19 | ERROR | Unable to retrieve data type of property "$args"
| | (ObjectCalisthenics.CodeAnalysis.InstancePropertyPerClassLimit.InvalidPropertyType)
From error message it's not clear:
In current form the message isn't either clear (what's the actual issue?) or actionable (what do I need to do to resolve it?).
PS same thing with rest of the methods in class, not just constructor.
I always get this when trying to run phpcs
.
I read the code and seems everything ok but the class Strings
doesn't exists.
Error: Class 'Nette\Utils\Strings' not found in /Users/user/Sites/sniffs/phpcs-calisthenics-rules/src/ObjectCalisthenics/Helper/Naming.php on line 43
Call Stack:
0.0006 392504 1. {main}() /usr/local/bin/phpcs.phar:0
0.0057 743384 2. PHP_CodeSniffer\Runner->runPHPCS() /usr/local/bin/phpcs.phar:6
0.1221 8372216 3. PHP_CodeSniffer\Runner->run() phar:///usr/local/bin/phpcs.phar/src/Runner.php:2
0.1291 8898144 4. PHP_CodeSniffer\Runner->processFile() phar:///usr/local/bin/phpcs.phar/src/Runner.php:2
0.1291 8898144 5. PHP_CodeSniffer\Files\LocalFile->process() phar:///usr/local/bin/phpcs.phar/src/Runner.php:2
0.1291 8898144 6. PHP_CodeSniffer\Files\LocalFile->process() phar:///usr/local/bin/phpcs.phar/src/Files/LocalFile.php:2
0.1516 9349616 7. ObjectCalisthenics\Sniffs\NamingConventions\ElementNameMinimalLengthSniff->process() phar:///usr/local/bin/phpcs.phar/src/Files/File.php:2
0.1521 9356928 8. ObjectCalisthenics\Helper\Naming::getElementName() /Users/user/Sites/sniffs/phpcs-calisthenics-rules/src/ObjectCalisthenics/Sniffs/NamingConventions/ElementNameMinimalLengthSniff.php:39
When running this phpcs standard on a project that also uses Doctrine annotations, it keeps getting false positives.
We need to update member comment processor and make it reliable.
If I use 2.* versions I got this error:
Fatal error: Class 'PHP_CodeSniffer_CommentParser_MemberCommentParser' not found
It would be nice to upgrade the version or at least to update composer.json with the real requirements of the project.
Hi there, could we get tags so that it can be installed with Composer? I'm too lazy to add the "minimum stability" flag to the global composer.json :) and I think anyway that having tags is a good thing right? What do you think?
What do you think about implement a small list of few accepted short variables like $id
?
I was trying to run calisthenics standard for pear package PHPCS but it is not working.
What is the best way to use this with pear package or standalone phar file?
When running phpcs-calisthenics-rules with PHP_CodeSniffer 2.0.0, i've the following error :
Fatal error: Class 'PHP_CodeSniffer_CommentParser_MemberCommentParser' not found in .composer/vendor/object-calisthenics/phpcs-calisthenics-rules/ObjectCalisthenics/PropertyTypePerClassLimitSniff.php on line 328
Phpcs and phpcs-calisthenics-rules installed globally through composer.
Hello guys,
I'm using phpcs-calisthenics-rules
with the PhpStorm inspector and when I do property overrides like this below I get an error like the second image with that content: https://gist.github.com/wilcorrea/93d37027c5fac6f7a74a566dab02bf93
If I change the visibility of property into the sniff class the "error disappeared"... but I believe it should be another way to override without receiving messages.
SO and PHP version
PHP 7.0.17-2+deb.sury.org~trusty+1 (cli) (built: Mar 15 2017 09:38:47) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.17-2+deb.sury.org~trusty+1, Copyright (c) 1999-2017, by Zend Technologies
Referenced sniff "ObjectCalisthenics.Files.ClassElementLength" does not exist
Hey there,
thanks for the awesome project.
Rule 9 is actually No getters/setters/properties (Source: Original by Jeff Bay). If you write getters and setters for everything, you are still exposing mutable state and you are still not being precise about how the member is supposed to be used (Nice code example here).
I think this is probably a case of cannot implement
or a warning whenever setters are encountered?
It seems like a standard method of suppression (@phpcsSuppress ObjectCalisthenics.Metrics.MethodPerClassLimit
) doesn't work when added to the class
.
Is it a known issue or is there a different method of suppressing that check for a particular class without excluding the whole file? (which in my case is marked for refactoring)
For this code:
$compiler // line 31
->subcompile( $this->getNode( 'body' ) )
->write( 'endwhile;' . "\n" );
I get this error:
31 | ERROR | Only one object operator per line.
| | (ObjectCalisthenics.CodeAnalysis.OneObjectOperatorPerLine)
Seems incorrect.
When I run the phpcs I get an error:
PHP Fatal error: Uncaught Error: Class 'SlevomatCodingStandard\Helpers\PropertyHelper' not found in /home/bley/PhpstormProjects/reflections/vendor/object-calisthenics/phpcs-calisthenics-rules/src/ObjectCalisthenics/Helper/ClassAnalyzer.php:57
slevomat/coding-standard
is just required as dev-dependency but seems to be necessary to run the sniffs.
Hi,
is there any way to get rid of the Only one object operator per line.
errors when using the Doctrine\DBAL\Query\QueryBuilder
?
Seems that something is wrong on the packages web hook as it don't contain the version v3.5.1: https://packagist.org/packages/object-calisthenics/phpcs-calisthenics-rules.
Can you trigger a manual update? Or maybe fix this?
Thanks!
Now that the new version of PHP CodeSniffer has been released is there anything that prevents a v3.0 stable release?
When creating phpcs rules, it should also enforced to follow them internally too.
The idea is to finish all 10 rules, then update codebase to meet them.
Roadmap:
That's a good exercise to show people how any code can be turned into compliant OC code.
Hye guys,
I'm trying to use a custom ruleset.xml.
./vendor/bin/phpcs -sp app tests --standard=ruleset.xml
but I got stuck on this error.
ERROR: Referenced sniff "ObjectCalisthenics.Metrics.MaxNestingLevel" does not exist
Cheers.
Perhaps I'm missing the obvious here...
Did a git clone, ran composer install
, then phpunit
:
RuntimeException: Could not find PHP_CodeSniffer test suite. Did you maybe forget to run composer installation with option "--prefer-source"? in /home/j/workspace/phpcs-calisthenics-rules/bootstrap.php on line 68
phpunit --version
PHPUnit 4.2.2 by Sebastian Bergmann.
commit 51b3b42
private function normalizeWriteCommand(
string $command,
$identifier,
$values = null,
array $options = []
): Couchbase\Document {
A function like that should probably be counted as 1 line and not 6 lines
It seems class doc comment processing was failing with the usage of Doctrine annotations.
We gotta fix it and turn back on.
I have in my composer.json
file:
"require-dev": {
"phpunit/phpunit": "4.*",
"squizlabs/php_codesniffer": "dev-master",
"object-calisthenics/phpcs-calisthenics-rules": "dev-master"
}
Raise the follow error
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Installation request for squizlabs/php_codesniffer dev-master -> satisfiable by squizlabs/php_codesniffer[dev-master].
- object-calisthenics/phpcs-calisthenics-rules dev-master requires squizlabs/php_codesniffer ~1.5 -> satisfiable by squizlabs/php_codesniffer[1.5.x-dev].
- Can only install one of: squizlabs/php_codesniffer[dev-master, 1.5.x-dev].
- Installation request for object-calisthenics/phpcs-calisthenics-rules dev-master -> satisfiable by object-calisthenics/phpcs-calisthenics-rules[dev-master]
When I replace version of php_codesniffer to ~1.5
its works.
I've got a situation where the production server has PHP 7.0 (limited to Object Calisthenics 2.*), but I want to use a Composer plugin for registering the PHP CodeSniffer standards in my project (requires OC define phpcodesniffer-standard
value in composer.json
).
Would it be possible to please add a 2.0.1 release that adds the required key to composer.json
, without bumping the minimum version of PHP?
On Helper/Structure/StructureMetrics::getStructureLengthInLines.
The function retrieves the difference between the opening and the ending position of the function body. But empty lines should count as code? Why not avoid them on the count?
It is about to be released in next month or so.
We could plan next release (3.0) along with it, so it matches the version.
And is compatible with other modern Sniffs.
I want to be more specific about types in my docBlock, for example:
/**
* @param string[] $bar
*/
public function Foo(array $bar) {
// ...
}
But it's actually raise a error on CS validation.
Expected type hint "string[]"; found "array" for $bat at position 1
Problem 1
- object-calisthenics/phpcs-calisthenics-rules v3.6.0 requires slevomat/coding-standard ^5.0.4 -> satisfiable by slevomat/coding-standard[5.0.4] but these conflict with your requirements or minimum-stability.
- object-calisthenics/phpcs-calisthenics-rules v3.5.1 requires slevomat/coding-standard ^5.0 -> satisfiable by slevomat/coding-standard[5.0.0, 5.0.1, 5.0.2, 5.0.3, 5.0.4] but these conflict with your requirements or minimum-stability.
- object-calisthenics/phpcs-calisthenics-rules v3.5.0 requires slevomat/coding-standard ^5.0 -> satisfiable by slevomat/coding-standard[5.0.0, 5.0.1, 5.0.2, 5.0.3, 5.0.4] but these conflict with your requirements or minimum-stability.
- object-calisthenics/phpcs-calisthenics-rules v3.6.0 requires slevomat/coding-standard ^5.0.4 -> satisfiable by slevomat/coding-standard[5.0.4] but these conflict with your requirements or minimum-stability.
- Installation request for object-calisthenics/phpcs-calisthenics-rules ^3.5 -> satisfiable by object-calisthenics/phpcs-calisthenics-rules[v3.5.0, v3.5.1, v3.6.0].
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.