Skip to content
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

Usage with phpcs pear package or standalone phpcs.phar #67

Closed
rohanvakharia opened this issue Jun 21, 2017 · 24 comments
Closed

Usage with phpcs pear package or standalone phpcs.phar #67

rohanvakharia opened this issue Jun 21, 2017 · 24 comments

Comments

@rohanvakharia
Copy link

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?

@TomasVotruba
Copy link
Contributor

TomasVotruba commented Jun 21, 2017

Hey, what exactly did you try? Code would be helpful

Usage is tested and works well

@rohanvakharia
Copy link
Author

Usage is tested Is tested for phpcs composer package. In the composer case we just need to install phpcs using composer require.

what about global phpcs package?
In the global case,

@TomasVotruba
Copy link
Contributor

I'm not familliar with using phar or pear, since it doesn't support reliable versioning as comoser.

Could you add test for that into .travis.yml under composer version test?

@rohanvakharia
Copy link
Author

Can you be able to guide on how do I use this standard with multiple projects without including in every project?

@TomasVotruba
Copy link
Contributor

What exactly do you mean?

@UFOMelkor
Copy link
Contributor

Is this what you are looking for? https://getcomposer.org/doc/03-cli.md#global

@rohanvakharia
Copy link
Author

This package is a custom standard for PHPCS. As per the documentation I need to require PHPCS and this package in every project to use the sniffs. If you have gone through the PHPCS composer package, I can use PHPCS in both ways global as well as local. So if I have phpcs installed globally, how can I install/ integrate this package / standard with PHPCS in a global way so that I don't have to add this package to all my project. I can directly use this standard from global installation?

@UFOMelkor
Copy link
Contributor

Run composer global require object-calisthenics/phpcs-calisthenics-rules and then
$COMPOSER_HOME/vendor/bin/phpcs src tests -sp --standard=$COMPOSER_HOME/vendor/object-calisthenics/phpcs-calisthenics-rules/src/ObjectCalisthenics/ruleset.xml
If $COMPOSER_HOME is not defined, please have a look at https://getcomposer.org/doc/03-cli.md#composer-home to find the composer home directory on your computer.

@rohanvakharia
Copy link
Author

I run the above command and received following error

PHP Fatal error:  Uncaught Error: Class 'Nette\Utils\Strings' not found in /home/rohan/.config/composer/vendor/object-calisthenics/phpcs-calisthenics-rules/src/ObjectCalisthenics/Helper/Naming.php:43
Stack trace:
#0 /home/rohan/.config/composer/vendor/object-calisthenics/phpcs-calisthenics-rules/src/ObjectCalisthenics/Sniffs/NamingConventions/ElementNameMinimalLengthSniff.php(40): ObjectCalisthenics\Helper\Naming::getElementName(Object(PHP_CodeSniffer\Files\LocalFile), 87)
#1 /usr/share/php/PHP/CodeSniffer/src/Files/File.php(431): ObjectCalisthenics\Sniffs\NamingConventions\ElementNameMinimalLengthSniff->process(Object(PHP_CodeSniffer\Files\LocalFile), 87)
#2 /usr/share/php/PHP/CodeSniffer/src/Files/LocalFile.php(94): PHP_CodeSniffer\Files\File->process()
#3 /usr/share/php/PHP/CodeSniffer/src/Runner.php(566): PHP_CodeSniffer\Files\LocalFile->process()
#4 /usr/share/php/PHP/CodeSniffer/src/Runner.php(383): PHP_CodeSniffer\Runner->processFile(Object(PHP_CodeSniffer\Files\LocalFile))
#5 /usr/share/php/PHP/CodeSniffer/src/Runner.php(112): PHP_CodeSniffer\R in /home/rohan/.config/composer/vendor/object-calisthenics/phpcs-calisthenics-rules/src/ObjectCalisthenics/Helper/Naming.php on line 43

As per the requirement I should have Nette\Utils package which was already installed via composer.

Here is the output of composer global show -i

guzzlehttp/guzzle                            6.2.0  Guzzle is a PHP HTTP client library
guzzlehttp/promises                          1.1.0  Guzzle promises library
guzzlehttp/psr7                              1.3.0  PSR-7 message implementation
laravel/installer                            v1.3.3 Laravel application installer.
nette/utils                                  v2.4.6 Nette Utility Classes
object-calisthenics/phpcs-calisthenics-rules v3.0.1 PHP CodeSniffer Object Calisthenics rules/sniffs
psr/http-message                             1.0    Common interface for HTTP messages
squizlabs/php_codesniffer                    3.0.1  PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined s...
symfony/console                              v3.0.6 Symfony Console Component
symfony/polyfill-mbstring                    v1.1.1 Symfony polyfill for the Mbstring extension
symfony/process                              v3.0.6 Symfony Process Component

How can I solve this?

@TomasVotruba
Copy link
Contributor

TomasVotruba commented Jun 23, 2017

@rohanvakharia I've tried the same "composer global" setup and it works for me.
I also see Nette\Utils is already there in your report.

What operating system do you use?

Not sure if bug in composer. Maybe ask in composer issues.

@rohanvakharia
Copy link
Author

Ubuntu 16.04

@UFOMelkor
Copy link
Contributor

Did you run /home/rohan/.config/composer/vendor/bin/phpcs src tests -sp --standard=/home/rohan/.config/composer/vendor/object-calisthenics/phpcs-calisthenics-rules/src/ObjectCalisthenics/ruleset.xml
or vendor/bin/phpcs src tests -sp --standard=/home/rohan/.config/composer/vendor/object-calisthenics/phpcs-calisthenics-rules/src/ObjectCalisthenics/ruleset.xml
?

@rohanvakharia
Copy link
Author

/home/rohan/.config/composer/vendor/bin/phpcs src tests -sp --standard=/home/rohan/.config/composer/vendor/object-calisthenics/phpcs-calisthenics-rules/src/ObjectCalisthenics/ruleset.xml

@TomasVotruba
Copy link
Contributor

Btw, I'd recommend using composer create-project ... approach, that will install project to standalone directory. There you have dependencies under control and you can use it in multiple other projects.

composer global ... is more different to use

@TomasVotruba
Copy link
Contributor

What's state is this in?

@rohanvakharia
Copy link
Author

I have multiple projects where I need to run these rules.
I tried different approaches to run it but couldn't succeed except adding in the project composer file.

I have installed a PHPCS standalone(phar file) in my bin folder. That gives me an easy access anywhere from terminal and in sublime as well using linter.

The way you are telling me to use that, I will need to add the package in each and every project which is not possible for me.

@TomasVotruba
Copy link
Contributor

TomasVotruba commented Jul 6, 2017

The way you are telling me to use that, I will need to add the package in each and every project...

I will try install it step by step.

FAIL 😢

1. Install It

# composer create-project <package-name> <destination-directory>
composer create-project object-calisthenics/phpcs-calisthenics-rules object-calisthenics-rules 

Now this package is located in /object-calisthenics-rules directory.

ruleset.xml is located in:

object-calisthenics-rules/src/ObjectCalisthenics/ruleset.xml

2. Run It with Own phar/bin

php_codesniffer/bin/phpcs standalone-directory/src -sp --standard=standalone-directory/src/ObjectCalisthenics/ruleset.xml

That fails with exception the same "Nette\Utils\Strings" exception:

image


This is caused by using extra classes that phpcs is unable to load.
That's why composer is needed at least at some way.

SOLUTION 🍰

When you use vendor/bin/phpcs that is in the same directory:

standalone-directory/vendor/bin/phpcs standalone-directory/src -sp --standard=standalone-directory/src/ObjectCalisthenics/ruleset.xml

All works fine:

image

And you can analyze any directory you need:

standalone-directory/vendor/bin/phpcs other-project -sp --standard=standalone-directory/src/ObjectCalisthenics/ruleset.xml

image

Can you try that?

@rohanvakharia
Copy link
Author

Thank you for detailed solution.

It is a very good work around for my problem.

I have tried this solution where I need to install PHPCS package using composer in a standalone directory followed by object calisthenics package which will install a Nette\Utils package as a dependecy.

Solution accepted.

Just a concern:
I was going through the code and found that you haven't used Nette\Utils package that extensively.
Can't you remove the dependency from your package which will make your package work for the failing use case which you explained to me?

@rohanvakharia
Copy link
Author

I can send you a PR if you don't mind.

@TomasVotruba
Copy link
Contributor

Solution accepted.

👍

Can't you remove the dependency from your package which will make your package work for the failing use case which you explained to me?

I though about this but though there are more external dependencies, that would have to be taken care of.
You can try that and if it works send PR. + We'll have to add a note about this to README for future generations to be careful, because adding dependencies is quite common.

I can send you a PR if you don't mind.

Sure, that would be great! 👏

@TomasVotruba
Copy link
Contributor

Do you have an idea when do you get to this?

@rohanvakharia
Copy link
Author

It won't take more than couple of days as I am working on client's project, will update you once it's done.

@TomasVotruba
Copy link
Contributor

Thanks for the info.

@TomasVotruba
Copy link
Contributor

Closing for time out. Feel free to reopen with PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants