Skip to content

Conversation

@stefanzweifel
Copy link
Owner

@stefanzweifel stefanzweifel commented Jul 29, 2020

This Pull Request adds a new feature to laravel-stats: a --share option. In addition, this PR allows the package to be installed in Laravel 6 applications again.

If you want to learn more about this branch and the added features, checkout these live streams:

If you want to test this branch in your project, you can install this "alpha" version by running.

composer require wnx/laravel-stats:dev-metrics --dev

As this PR is quite big, let's go through the changes one by one.

Add back support for Laravel 6.*

Back in March 2020, I've released v2.1.0 and with it removed support for Laravel 6. Due to the changes made to one of our core dependencies – phploc/phploc - this was the straightforward solution.

However, the newly added --share-feature and its dashboard will benefit if Laravel 6 user can use it too. 6.0 is also an LTS, so a lot more people are on that version.

To make this possible, I've forked the phploc-package and made it compatible for both Laravel 6 and Laravel 7.

The fork can be found here. I do not recommend installing this package in any of your projects as a direct dependency. When Laravel 6 download numbers fall, I will switch back to the "normal" phploc/phploc-version.

The --share option

Jason McCreary, the creator of Shift, and I had an idea 2 years ago after Jason gave his "Laravel by the Numbers" talk at Laracon US 2018: How can be safely collect project statistics, analyze them and present them to the community in an easy-to-read way?

This new option is part one of the solution we came up with. Part two will be a open source repository we're going to release soon. In there you can inspect the code and see, how the data is grouped and displayed.

Enough back story. Here's a quick overview how this feature has been implemented

StatsCommand

The stats artisan command has been extended with several new options:

  • --s|share: Indicate that you would like to share project statistics with the Laravel Community
  • --name: Overrides the project name which the stats command would try to determine itself. (Useful if you want to use the --share option in a CI setting
  • --payload: Passing this option will output the payload to the console, the command would send to the remote Share API.
  • --dry-run: Passing this option prevents the command from actually making the request to the Share API.

A note regarding the --name-option: In order to make sure, that your project statistics are counted only once, the Share API needs to uniquely identify your project. We chose the simplest way of giving your project a name.

If you run php artisan stats --share the command will try to determine the project name based on the remote repository name stored in your local git configuration. The command will ask you to confirm or update the name.

The name will be stored in a .laravelstatsrc-file. If the file exists and has a name in it, the command won't ask you for the project name again.

Shareable Metrics

The code for the new feature can be found in src/ShareableMetrics. In there you will find a list of metrics the command will send to the Share API.
In addition to the "core" metrics the stats command shows you in the console, we've written a few new ones which we think are interesting. Like:

  • models_mass_assignment: This metric determines if you're using more fillable or guarded in your Models.
  • controllers_custom_inheritance: This metric determines if your controllers extend other controllers (for example: All your API controllers extends a BaseApiController).
  • controllers_form_request_injection: This metrics determines, if your using Form Requests in your controllers.

The code for these metrics might look intimidating. In their core, they are looping through a collection of classes in your project and use the PHP reflection API to determine if they conform to a given measured metric.

In the future, I would like to refactor the code for these metrics, to make them easier to understand for new comers to the package.


We hope that the community likes the idea we had too.
We hope that the anonymously shared data will help us build better and more robust applications.


TODOs

  • Update README to include details about --share
  • Add GitHub Action example to run --share on CI

@codecov
Copy link

codecov bot commented Jul 29, 2020

Codecov Report

Merging #178 into master will decrease coverage by 3.44%.
The diff coverage is 84.83%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #178      +/-   ##
============================================
- Coverage     96.35%   92.90%   -3.45%     
- Complexity      241      318      +77     
============================================
  Files            42       62      +20     
  Lines           685      987     +302     
============================================
+ Hits            660      917     +257     
- Misses           25       70      +45     
Impacted Files Coverage Δ Complexity Δ
src/Classifier.php 100.00% <ø> (ø) 7.00 <0.00> (ø)
src/ShareableMetrics/ProjectName.php 0.00% <0.00%> (ø) 13.00 <13.00> (?)
src/ShareableMetrics/SendToLaravelShift.php 26.66% <26.66%> (ø) 4.00 <4.00> (?)
src/Console/StatsListCommand.php 82.81% <79.54%> (-9.50%) 22.00 <18.00> (+16.00) ⬇️
src/Classifiers/BladeComponentClassifier.php 100.00% <100.00%> (ø) 5.00 <0.00> (+1.00)
src/Classifiers/CustomCastClassifier.php 100.00% <100.00%> (ø) 6.00 <0.00> (+1.00)
src/Classifiers/ObserverClassifier.php 86.66% <100.00%> (+6.66%) 6.00 <0.00> (ø)
src/ShareableMetrics/AllMetricPayloadKeys.php 100.00% <100.00%> (ø) 4.00 <4.00> (?)
src/ShareableMetrics/CollectMetrics.php 100.00% <100.00%> (ø) 4.00 <4.00> (?)
src/ShareableMetrics/Metrics/CodeTestRatio.php 100.00% <100.00%> (ø) 2.00 <2.00> (?)
... and 36 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 02c0e50...952540f. Read the comment docs.

stefanzweifel and others added 5 commits August 24, 2020 20:15
This Class is mostly used for internal development purposes.
# Conflicts:
#	tests/ValueObjects/ClassifiedClassTest.php
#	tests/ValueObjects/ComponentTest.php
@stefanzweifel stefanzweifel merged commit 5db183c into master Sep 1, 2020
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

Successfully merging this pull request may close these issues.

3 participants