From be0ba35b9e8f9c62cd9d487875670b0d5035a3dd Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Tue, 22 Mar 2022 19:31:51 +0100 Subject: [PATCH 1/5] Remove ShareableMetrics --- src/ShareableMetrics/AllMetricPayloadKeys.php | 52 ----------- src/ShareableMetrics/CollectMetrics.php | 93 ------------------- .../Metrics/CodeTestRatio.php | 18 ---- .../Metrics/ComposerPsr4Sources.php | 22 ----- .../Metrics/ControllersCustomInheritance.php | 46 --------- .../ControllersFormRequestInjection.php | 48 ---------- .../Metrics/InstalledPackages.php | 28 ------ src/ShareableMetrics/Metrics/Metric.php | 25 ----- .../Metrics/ModelsCustomInheritance.php | 46 --------- src/ShareableMetrics/Metrics/ModelsFolder.php | 39 -------- .../Metrics/ModelsMassAssignment.php | 44 --------- .../Metrics/NumberOfRoutes.php | 19 ---- .../Metrics/ProjectLinesOfCode.php | 18 ---- .../Metrics/ProjectLogicalLinesOfCode.php | 18 ---- .../ProjectLogicalLinesOfCodePerMethod.php | 18 ---- .../Metrics/ProjectNumberOfClasses.php | 18 ---- .../Metrics/ScheduledTasks.php | 19 ---- src/ShareableMetrics/MetricsCollection.php | 22 ----- src/ShareableMetrics/ProjectName.php | 86 ----------------- src/ShareableMetrics/SendToLaravelShift.php | 40 -------- .../AllMetricPayloadKeysTest.php | 24 ----- .../Metrics/CodeTestRatioTest.php | 38 -------- .../Metrics/ComposerPsr4SourcesTest.php | 37 -------- .../ControllersCustomInheritanceTest.php | 65 ------------- .../ControllersFormRequestInjectionTest.php | 50 ---------- .../Metrics/InstalledPackagesTest.php | 42 --------- .../Metrics/ModelsCustomInheritanceTest.php | 58 ------------ .../Metrics/ModelsFolderTest.php | 44 --------- .../Metrics/ModelsMassAssignmentTest.php | 49 ---------- .../Metrics/NumberOfRoutesTest.php | 35 ------- .../Metrics/ProjectLinesOfCodeTest.php | 38 -------- .../Metrics/ProjectLogicalLinesOfCodeTest.php | 38 -------- .../Metrics/ProjectNumberOfClassesTest.php | 38 -------- .../Metrics/ScheduledTasksTest.php | 45 --------- 34 files changed, 1320 deletions(-) delete mode 100644 src/ShareableMetrics/AllMetricPayloadKeys.php delete mode 100644 src/ShareableMetrics/CollectMetrics.php delete mode 100644 src/ShareableMetrics/Metrics/CodeTestRatio.php delete mode 100644 src/ShareableMetrics/Metrics/ComposerPsr4Sources.php delete mode 100644 src/ShareableMetrics/Metrics/ControllersCustomInheritance.php delete mode 100644 src/ShareableMetrics/Metrics/ControllersFormRequestInjection.php delete mode 100644 src/ShareableMetrics/Metrics/InstalledPackages.php delete mode 100644 src/ShareableMetrics/Metrics/Metric.php delete mode 100644 src/ShareableMetrics/Metrics/ModelsCustomInheritance.php delete mode 100644 src/ShareableMetrics/Metrics/ModelsFolder.php delete mode 100644 src/ShareableMetrics/Metrics/ModelsMassAssignment.php delete mode 100644 src/ShareableMetrics/Metrics/NumberOfRoutes.php delete mode 100644 src/ShareableMetrics/Metrics/ProjectLinesOfCode.php delete mode 100644 src/ShareableMetrics/Metrics/ProjectLogicalLinesOfCode.php delete mode 100644 src/ShareableMetrics/Metrics/ProjectLogicalLinesOfCodePerMethod.php delete mode 100644 src/ShareableMetrics/Metrics/ProjectNumberOfClasses.php delete mode 100644 src/ShareableMetrics/Metrics/ScheduledTasks.php delete mode 100644 src/ShareableMetrics/MetricsCollection.php delete mode 100644 src/ShareableMetrics/ProjectName.php delete mode 100644 src/ShareableMetrics/SendToLaravelShift.php delete mode 100644 tests/ShareableMetrics/AllMetricPayloadKeysTest.php delete mode 100644 tests/ShareableMetrics/Metrics/CodeTestRatioTest.php delete mode 100644 tests/ShareableMetrics/Metrics/ComposerPsr4SourcesTest.php delete mode 100644 tests/ShareableMetrics/Metrics/ControllersCustomInheritanceTest.php delete mode 100644 tests/ShareableMetrics/Metrics/ControllersFormRequestInjectionTest.php delete mode 100644 tests/ShareableMetrics/Metrics/InstalledPackagesTest.php delete mode 100644 tests/ShareableMetrics/Metrics/ModelsCustomInheritanceTest.php delete mode 100644 tests/ShareableMetrics/Metrics/ModelsFolderTest.php delete mode 100644 tests/ShareableMetrics/Metrics/ModelsMassAssignmentTest.php delete mode 100644 tests/ShareableMetrics/Metrics/NumberOfRoutesTest.php delete mode 100644 tests/ShareableMetrics/Metrics/ProjectLinesOfCodeTest.php delete mode 100644 tests/ShareableMetrics/Metrics/ProjectLogicalLinesOfCodeTest.php delete mode 100644 tests/ShareableMetrics/Metrics/ProjectNumberOfClassesTest.php delete mode 100644 tests/ShareableMetrics/Metrics/ScheduledTasksTest.php diff --git a/src/ShareableMetrics/AllMetricPayloadKeys.php b/src/ShareableMetrics/AllMetricPayloadKeys.php deleted file mode 100644 index b7eefeae..00000000 --- a/src/ShareableMetrics/AllMetricPayloadKeys.php +++ /dev/null @@ -1,52 +0,0 @@ -getProjectMetricKeys() - ->merge($this->getComponentMetricKeys()) - ->toArray(); - } - - private function getProjectMetricKeys(): Collection - { - return collect(CollectMetrics::PROJECT_METRICS) - ->map(function ($metricClass) { - return new $metricClass(new Project(collect([]))); - }) - ->map(function ($metric) { - return $metric->name(); - }); - } - - private function getComponentMetricKeys(): Collection - { - $coreClassifierNames = array_map(function ($classifier) { - return (new $classifier)->name(); - }, Classifier::DEFAULT_CLASSIFIER); - - $metrics = []; - - foreach ($coreClassifierNames as $coreClassifier) { - $slug = Str::slug(strtolower($coreClassifier), '_'); - - $metrics[] = [ - "{$slug}", - "{$slug}_methods", - "{$slug}_loc", - "{$slug}_lloc", - "{$slug}_lloc_per_method", - ]; - } - - return collect($metrics)->flatten(); - } -} diff --git a/src/ShareableMetrics/CollectMetrics.php b/src/ShareableMetrics/CollectMetrics.php deleted file mode 100644 index 730748ab..00000000 --- a/src/ShareableMetrics/CollectMetrics.php +++ /dev/null @@ -1,93 +0,0 @@ - $this->getProjectMetrics($project), - 'component_metrics' => $this->getComponentMetrics($project) - ]); - } - - protected function getProjectMetrics(Project $project): Collection - { - return collect(self::PROJECT_METRICS) - ->map(function (string $metricClass) use ($project) { - return new $metricClass($project); - }) - ->map(function (Metric $metric) { - return $metric->toArray(); - }) - ->collapse(); - } - - protected function getComponentMetrics(Project $project): Collection - { - // Get the Names of "Core"-Components - $coreClassifierNames = array_map(function ($classifier) { - return (new $classifier)->name(); - }, Classifier::DEFAULT_CLASSIFIER); - - // Group Into Components - $groupedByComponent = $project->classifiedClassesGroupedAndFilteredByComponentNames($coreClassifierNames) - ->map(function ($classifiedClasses, $componentName) { - return new Component($componentName, $classifiedClasses); - }); - - $metrics = []; - - /** @var Component $component */ - foreach ($groupedByComponent as $component) { - $slug = Str::slug(strtolower($component->name), '_'); - - $metrics["{$slug}"] = $component->getNumberOfClasses(); - $metrics["{$slug}_methods"] = $component->getNumberOfMethods(); - $metrics["{$slug}_loc"] = $component->getLinesOfCode(); - $metrics["{$slug}_lloc"] = $component->getLogicalLinesOfCode(); - $metrics["{$slug}_lloc_per_method"] = $component->getLogicalLinesOfCodePerMethod(); - } - - return collect($metrics); - } -} diff --git a/src/ShareableMetrics/Metrics/CodeTestRatio.php b/src/ShareableMetrics/Metrics/CodeTestRatio.php deleted file mode 100644 index 8720908c..00000000 --- a/src/ShareableMetrics/Metrics/CodeTestRatio.php +++ /dev/null @@ -1,18 +0,0 @@ -project->statistic()->getApplicationCodeToTestCodeRatio(); - } -} diff --git a/src/ShareableMetrics/Metrics/ComposerPsr4Sources.php b/src/ShareableMetrics/Metrics/ComposerPsr4Sources.php deleted file mode 100644 index 98eec15c..00000000 --- a/src/ShareableMetrics/Metrics/ComposerPsr4Sources.php +++ /dev/null @@ -1,22 +0,0 @@ -project - ->classifiedClasses() - ->filter(function (ClassifiedClass $classifiedClass) { - return $classifiedClass->classifier->name() === 'Controllers'; - }); - - if ($controllers->count() === 0) { - return null; - } - - return $controllers - ->reject(function (ClassifiedClass $classifiedClass) { - return $classifiedClass->reflectionClass->getParentClass() === false; - }) - - // Remove Controllers, which extend a Class which is located in the vendor folder - ->reject(function (ClassifiedClass $classifiedClass) { - $parentclass = new ReflectionClass($classifiedClass->reflectionClass->getParentClass()->getName()); - - return $parentclass->isVendorProvided(); - }) - ->reject(function (ClassifiedClass $classifiedClass) { - $parentClassName = $classifiedClass->reflectionClass->getParentClass()->getShortName(); - - return $parentClassName === 'Controller'; - }) - ->count() > 0; - } -} diff --git a/src/ShareableMetrics/Metrics/ControllersFormRequestInjection.php b/src/ShareableMetrics/Metrics/ControllersFormRequestInjection.php deleted file mode 100644 index 3f74681f..00000000 --- a/src/ShareableMetrics/Metrics/ControllersFormRequestInjection.php +++ /dev/null @@ -1,48 +0,0 @@ -project - ->classifiedClasses() - ->filter(function (ClassifiedClass $classifiedClass) { - return $classifiedClass->classifier->name() === 'Controllers'; - }) - ->flatMap(function (ClassifiedClass $classifiedClass) { - return collect($classifiedClass->reflectionClass->getDefinedMethods()); - }) - ->filter(function (ReflectionMethod $method) { - return count($method->getParameters()) > 0; - }) - ->flatMap(function (ReflectionMethod $method) { - return collect($method->getParameters()); - }) - ->filter(function (ReflectionParameter $param) { - return $param->hasType(); - }) - ->reject(function (ReflectionParameter $param) { - return $param->getType()->isBuiltin(); - }) - ->filter(function (ReflectionParameter $param) { - $reflectionClass = new ReflectionClass($param->getType()->getName()); - - return app(RequestClassifier::class)->satisfies($reflectionClass); - }) - ->count() > 0; - } -} diff --git a/src/ShareableMetrics/Metrics/InstalledPackages.php b/src/ShareableMetrics/Metrics/InstalledPackages.php deleted file mode 100644 index 2ccb280a..00000000 --- a/src/ShareableMetrics/Metrics/InstalledPackages.php +++ /dev/null @@ -1,28 +0,0 @@ - $packages, - 'require-dev' => $devPackages, - ]; - } -} diff --git a/src/ShareableMetrics/Metrics/Metric.php b/src/ShareableMetrics/Metrics/Metric.php deleted file mode 100644 index 544c45cd..00000000 --- a/src/ShareableMetrics/Metrics/Metric.php +++ /dev/null @@ -1,25 +0,0 @@ -project = $project; - } - - public function toArray() - { - return [ - $this->name() => $this->value() - ]; - } -} diff --git a/src/ShareableMetrics/Metrics/ModelsCustomInheritance.php b/src/ShareableMetrics/Metrics/ModelsCustomInheritance.php deleted file mode 100644 index d0c43961..00000000 --- a/src/ShareableMetrics/Metrics/ModelsCustomInheritance.php +++ /dev/null @@ -1,46 +0,0 @@ -project - ->classifiedClasses() - ->filter(function (ClassifiedClass $classifiedClass) { - return $classifiedClass->classifier->name() === 'Models'; - }); - - if ($models->count() === 0) { - return null; - } - - return $models - - // Remove Models, which extend a Class which is located in the vendor folder - ->reject(function (ClassifiedClass $classifiedClass) { - $parentclass = new ReflectionClass($classifiedClass->reflectionClass->getParentClass()->getName()); - - return $parentclass->isVendorProvided(); - }) - ->reject(function (ClassifiedClass $classifiedClass) { - $parentClassName = $classifiedClass->reflectionClass->getParentClass()->getName(); - - // If a Model extends an Illuminate-class, remove it from the collection - // as we see it as a "normal" Model - return Str::startsWith($parentClassName, 'Illuminate'); - }) - ->count() > 0; - } -} diff --git a/src/ShareableMetrics/Metrics/ModelsFolder.php b/src/ShareableMetrics/Metrics/ModelsFolder.php deleted file mode 100644 index 01d1f428..00000000 --- a/src/ShareableMetrics/Metrics/ModelsFolder.php +++ /dev/null @@ -1,39 +0,0 @@ -project - ->classifiedClasses() - ->filter(function (ClassifiedClass $classifiedClass) { - return $classifiedClass->classifier->name() === 'Models'; - }); - - if ($models->count() === 0) { - return null; - } - - return $models - ->map(function (ClassifiedClass $classifiedClass) { - return $classifiedClass->reflectionClass->getNamespaceName(); - }) - ->filter(function (string $namespace) { - // If a Models namespace contains a back-slash, we assume that the Model - // is not located in the default location under /app, but somewhere else - return Str::contains($namespace, "\\"); - }) - ->count() > 0; - } -} diff --git a/src/ShareableMetrics/Metrics/ModelsMassAssignment.php b/src/ShareableMetrics/Metrics/ModelsMassAssignment.php deleted file mode 100644 index 5f864acc..00000000 --- a/src/ShareableMetrics/Metrics/ModelsMassAssignment.php +++ /dev/null @@ -1,44 +0,0 @@ -project - ->classifiedClasses() - ->filter(function (ClassifiedClass $classifiedClass) { - return $classifiedClass->classifier->name() === 'Models'; - }) - ->map(function (ClassifiedClass $classifiedClass) { - - // Collect all properties of the given class, - // filter to only keep "guarded" and "fillable" - // and only keep those which have been set by the developer. - return collect($classifiedClass->reflectionClass->getProperties()) - ->filter(function (ReflectionProperty $property) { - return in_array($property->getName(), ['guarded', 'fillable']); - }) - ->filter(function (ReflectionProperty $property) use ($classifiedClass) { - return $property->class === $classifiedClass->reflectionClass->name; - }) - ->toArray(); - }) - ->filter() - ->flatten(1) - ->countBy(function (ReflectionProperty $property) { - return $property->getName(); - }) - ->toArray(); - } -} diff --git a/src/ShareableMetrics/Metrics/NumberOfRoutes.php b/src/ShareableMetrics/Metrics/NumberOfRoutes.php deleted file mode 100644 index 7b7e262e..00000000 --- a/src/ShareableMetrics/Metrics/NumberOfRoutes.php +++ /dev/null @@ -1,19 +0,0 @@ -get(); - } -} diff --git a/src/ShareableMetrics/Metrics/ProjectLinesOfCode.php b/src/ShareableMetrics/Metrics/ProjectLinesOfCode.php deleted file mode 100644 index c1812ca8..00000000 --- a/src/ShareableMetrics/Metrics/ProjectLinesOfCode.php +++ /dev/null @@ -1,18 +0,0 @@ -project->statistic()->getLinesOfCode(); - } -} diff --git a/src/ShareableMetrics/Metrics/ProjectLogicalLinesOfCode.php b/src/ShareableMetrics/Metrics/ProjectLogicalLinesOfCode.php deleted file mode 100644 index 9cc9c38e..00000000 --- a/src/ShareableMetrics/Metrics/ProjectLogicalLinesOfCode.php +++ /dev/null @@ -1,18 +0,0 @@ -project->statistic()->getLogicalLinesOfCode(); - } -} diff --git a/src/ShareableMetrics/Metrics/ProjectLogicalLinesOfCodePerMethod.php b/src/ShareableMetrics/Metrics/ProjectLogicalLinesOfCodePerMethod.php deleted file mode 100644 index acc32d32..00000000 --- a/src/ShareableMetrics/Metrics/ProjectLogicalLinesOfCodePerMethod.php +++ /dev/null @@ -1,18 +0,0 @@ -project->statistic()->getLogicalLinesOfCodePerMethod(); - } -} diff --git a/src/ShareableMetrics/Metrics/ProjectNumberOfClasses.php b/src/ShareableMetrics/Metrics/ProjectNumberOfClasses.php deleted file mode 100644 index 317d525b..00000000 --- a/src/ShareableMetrics/Metrics/ProjectNumberOfClasses.php +++ /dev/null @@ -1,18 +0,0 @@ -project->statistic()->getNumberOfClasses(); - } -} diff --git a/src/ShareableMetrics/Metrics/ScheduledTasks.php b/src/ShareableMetrics/Metrics/ScheduledTasks.php deleted file mode 100644 index 17a0497d..00000000 --- a/src/ShareableMetrics/Metrics/ScheduledTasks.php +++ /dev/null @@ -1,19 +0,0 @@ -events()); - } -} diff --git a/src/ShareableMetrics/MetricsCollection.php b/src/ShareableMetrics/MetricsCollection.php deleted file mode 100644 index 3adae10b..00000000 --- a/src/ShareableMetrics/MetricsCollection.php +++ /dev/null @@ -1,22 +0,0 @@ -get('project_metrics'); - $componentMetrics = $this->get('component_metrics'); - - return [ - 'project' => $projectName, - 'metrics' => $projectMetrics - ->merge($componentMetrics) - ->sortKeys() - ->toArray() - ]; - } -} diff --git a/src/ShareableMetrics/ProjectName.php b/src/ShareableMetrics/ProjectName.php deleted file mode 100644 index f64724fa..00000000 --- a/src/ShareableMetrics/ProjectName.php +++ /dev/null @@ -1,86 +0,0 @@ -hasStoredProjectName()) { - return File::get($this->pathToRcFile()); - } - - return null; - } - - public function determineProjectNameFromGit(): ?string - { - $gitPath = $this->getGitBinaryPath(); - - if (is_null($gitPath)) { - return null; - } - - $process = Process::fromShellCommandline("{$gitPath} config --get remote.origin.url"); - $process->run(); - - if (!$process->isSuccessful()) { - return null; - } - - $remoteUrl = trim($process->getOutput()); - - // HTTPS Connections - if (Str::startsWith($remoteUrl, 'http')) { - $remoteUrl = parse_url($remoteUrl); - $remoteUrl = Str::replaceLast('.git', '', $remoteUrl['path']); - - return Str::replaceFirst('/', '', $remoteUrl); - } - - // SSH Connections - $remoteUrl = parse_url($remoteUrl); - $remoteUrl = Str::replaceLast('.git', '', $remoteUrl['path']); - - [$_, $remoteUrl] = explode(':', $remoteUrl); - - return $remoteUrl; - } - - protected function getGitBinaryPath(): ?string - { - $process = Process::fromShellCommandline('which git'); - $process->run(); - - if (!$process->isSuccessful()) { - return null; - } - - return trim($process->getOutput()); - } - - protected function pathToRcFile(): string - { - return base_path(self::RC_FILE); - } - - public function hasStoredProjectName(): bool - { - return File::exists($this->pathToRcFile()); - } - - public function storeNameInRcFile(string $projectName): void - { - if (app()->environment('testing')) { - return; - } - - File::put($this->pathToRcFile(), $projectName); - } -} diff --git a/src/ShareableMetrics/SendToLaravelShift.php b/src/ShareableMetrics/SendToLaravelShift.php deleted file mode 100644 index 87873907..00000000 --- a/src/ShareableMetrics/SendToLaravelShift.php +++ /dev/null @@ -1,40 +0,0 @@ -environment('testing')) { - return true; - } - - try { - $client = new Client([ - 'base_uri' => 'https://laravelshift.com', - 'timeout' => 5, - 'headers' => ['Accept' => 'application/json'], - ]); - - $response = $client->request('POST', '/api/stat', [ - 'json' => $payload, - ]); - } catch (GuzzleException $exception) { - info('Unable to share stats: ' . $exception->getMessage()); - return false; - } - - if (in_array($response->getStatusCode(), [201, 204])) { - info("Thanks for sharing your project data with the community!"); - return true; - } - - return false; - } -} diff --git a/tests/ShareableMetrics/AllMetricPayloadKeysTest.php b/tests/ShareableMetrics/AllMetricPayloadKeysTest.php deleted file mode 100644 index a3806216..00000000 --- a/tests/ShareableMetrics/AllMetricPayloadKeysTest.php +++ /dev/null @@ -1,24 +0,0 @@ -get(); - - $this->assertIsArray($result); - - $this->assertContains('packages', $result); - $this->assertContains('routes', $result); - $this->assertContains('models_mass_assignment', $result); - $this->assertContains('controllers_lloc_per_method', $result); - $this->assertContains('nova_lenses_methods', $result); - $this->assertContains('database_factories_lloc', $result); - } -} diff --git a/tests/ShareableMetrics/Metrics/CodeTestRatioTest.php b/tests/ShareableMetrics/Metrics/CodeTestRatioTest.php deleted file mode 100644 index e9f3e350..00000000 --- a/tests/ShareableMetrics/Metrics/CodeTestRatioTest.php +++ /dev/null @@ -1,38 +0,0 @@ -createProjectFromClasses([]); - - $metric = new CodeTestRatio($project); - - $this->assertEquals('code_test_ratio', $metric->name()); - } - - /** @test */ - public function it_returns_ratio_of_app_and_test_code() - { - $project = $this->createProjectFromClasses([ - User::class, - DemoDuskTest::class, - DemoUnitTest::class, - DemoBrowserKit::class, - ]); - - $metric = new CodeTestRatio($project); - - $this->assertEquals(0.6, $metric->value()); - } -} diff --git a/tests/ShareableMetrics/Metrics/ComposerPsr4SourcesTest.php b/tests/ShareableMetrics/Metrics/ComposerPsr4SourcesTest.php deleted file mode 100644 index 587ccf79..00000000 --- a/tests/ShareableMetrics/Metrics/ComposerPsr4SourcesTest.php +++ /dev/null @@ -1,37 +0,0 @@ -createProjectFromClasses([]); - - $metric = new ComposerPsr4Sources($project); - - $this->assertEquals('composer_psr_4_namespaces', $metric->name()); - } - - /** @test */ - public function it_returns_composer_psr_4_sources() - { - $project = $this->createProjectFromClasses([]); - - $metric = new ComposerPsr4Sources($project); - - $value = $metric->value(); - - $this->assertIsArray($value); - - // dd($value); - - $this->assertEquals([ - "App\\" => 'app/' - ], $value); - } -} diff --git a/tests/ShareableMetrics/Metrics/ControllersCustomInheritanceTest.php b/tests/ShareableMetrics/Metrics/ControllersCustomInheritanceTest.php deleted file mode 100644 index 7dc1637d..00000000 --- a/tests/ShareableMetrics/Metrics/ControllersCustomInheritanceTest.php +++ /dev/null @@ -1,65 +0,0 @@ -createProjectFromClasses([]); - - $metric = new ControllersCustomInheritance($project); - - $this->assertEquals('controllers_custom_inheritance', $metric->name()); - } - - /** @test */ - public function it_returns_true_if_controller_extends_another_controller_in_the_project() - { - Route::get('posts', [PostsController::class, 'index']); - - $project = $this->createProjectFromClasses([ - PostsController::class - ]); - - $metric = new ControllersCustomInheritance($project); - - $this->assertTrue($metric->value()); - } - - /** @test */ - public function it_returns_false_if_controller_does_not_extend_another_controller_in_the_project() - { - Route::get('projects', [ProjectsController::class, 'index']); - - $project = $this->createProjectFromClasses([ - ProjectsController::class - ]); - - $metric = new ControllersCustomInheritance($project); - - $this->assertFalse($metric->value()); - } - - /** @test */ - public function it_returns_false_if_controller_extends_the_default_laravel_controller() - { - Route::get('users', [UsersController::class, 'index']); - - $project = $this->createProjectFromClasses([ - UsersController::class - ]); - - $metric = new ControllersCustomInheritance($project); - - $this->assertFalse($metric->value()); - } -} diff --git a/tests/ShareableMetrics/Metrics/ControllersFormRequestInjectionTest.php b/tests/ShareableMetrics/Metrics/ControllersFormRequestInjectionTest.php deleted file mode 100644 index 29c1c286..00000000 --- a/tests/ShareableMetrics/Metrics/ControllersFormRequestInjectionTest.php +++ /dev/null @@ -1,50 +0,0 @@ -createProjectFromClasses([]); - - $metric = new ControllersFormRequestInjection($project); - - $this->assertEquals('controllers_form_request_injection', $metric->name()); - } - - /** @test */ - public function it_returns_true_if_controllers_do_use_form_request_injection() - { - Route::get('users', [UsersController::class, 'index']); - - $project = $this->createProjectFromClasses([ - UsersController::class - ]); - - $metric = new ControllersFormRequestInjection($project); - - $this->assertTrue($metric->value()); - } - - /** @test */ - public function it_returns_false_if_controllers_do_not_use_form_request_injection() - { - Route::get('projects', [ProjectsController::class, 'index']); - - $project = $this->createProjectFromClasses([ - ProjectsController::class - ]); - - $metric = new ControllersFormRequestInjection($project); - - $this->assertFalse($metric->value()); - } -} diff --git a/tests/ShareableMetrics/Metrics/InstalledPackagesTest.php b/tests/ShareableMetrics/Metrics/InstalledPackagesTest.php deleted file mode 100644 index 901d608b..00000000 --- a/tests/ShareableMetrics/Metrics/InstalledPackagesTest.php +++ /dev/null @@ -1,42 +0,0 @@ -createProjectFromClasses([]); - - $metric = new InstalledPackages($project); - - $this->assertEquals('packages', $metric->name()); - } - - /** @test */ - public function it_returns_array_of_installed_packages_for_the_project() - { - $project = $this->createProjectFromClasses([]); - - $metric = new InstalledPackages($project); - - $value = $metric->value(); - - $this->assertIsArray($value); - $this->assertArrayHasKey('require', $value); - $this->assertArrayHasKey('require-dev', $value); - - $this->assertEquals([ - 'require' => [ - 'laravel/framework' => '~5.0' - ], - 'require-dev' => [ - 'phpunit/phpunit' => '~4.0' - ] - ], $value); - } -} diff --git a/tests/ShareableMetrics/Metrics/ModelsCustomInheritanceTest.php b/tests/ShareableMetrics/Metrics/ModelsCustomInheritanceTest.php deleted file mode 100644 index 592e6628..00000000 --- a/tests/ShareableMetrics/Metrics/ModelsCustomInheritanceTest.php +++ /dev/null @@ -1,58 +0,0 @@ -createProjectFromClasses([]); - - $metric = new ModelsCustomInheritance($project); - - $this->assertEquals('models_custom_inheritance', $metric->name()); - } - - /** @test */ - public function it_returns_null_if_no_models_are_found_in_the_project() - { - $project = $this->createProjectFromClasses([ - // - ]); - - $metric = new ModelsCustomInheritance($project); - - $this->assertNull($metric->value()); - } - - - /** @test */ - public function it_returns_true_if_models_extends_another_model_in_the_project() - { - $project = $this->createProjectFromClasses([ - Group::class - ]); - - $metric = new ModelsCustomInheritance($project); - - $this->assertTrue($metric->value()); - } - - /** @test */ - public function it_returns_false_if_models_does_not_extend_another_model_in_the_project() - { - $project = $this->createProjectFromClasses([ - User::class - ]); - - $metric = new ModelsCustomInheritance($project); - - $this->assertFalse($metric->value()); - } -} diff --git a/tests/ShareableMetrics/Metrics/ModelsFolderTest.php b/tests/ShareableMetrics/Metrics/ModelsFolderTest.php deleted file mode 100644 index 5d8bef5f..00000000 --- a/tests/ShareableMetrics/Metrics/ModelsFolderTest.php +++ /dev/null @@ -1,44 +0,0 @@ -createProjectFromClasses([]); - - $metric = new ModelsFolder($project); - - $this->assertEquals('models_folder', $metric->name()); - } - - /** @test */ - public function it_returns_null_if_no_models_are_found_in_the_project() - { - $project = $this->createProjectFromClasses([ - // - ]); - - $metric = new ModelsFolder($project); - - $this->assertNull($metric->value()); - } - - /** @test */ - public function it_returns_true_if_models_are_stored_in_non_default_namespace() - { - $project = $this->createProjectFromClasses([ - User::class, - ]); - - $metric = new ModelsFolder($project); - - $this->assertTrue($metric->value()); - } -} diff --git a/tests/ShareableMetrics/Metrics/ModelsMassAssignmentTest.php b/tests/ShareableMetrics/Metrics/ModelsMassAssignmentTest.php deleted file mode 100644 index b2095d0d..00000000 --- a/tests/ShareableMetrics/Metrics/ModelsMassAssignmentTest.php +++ /dev/null @@ -1,49 +0,0 @@ -createProjectFromClasses([]); - - $metric = new ModelsMassAssignment($project); - - $this->assertEquals('models_mass_assignment', $metric->name()); - } - - /** @test */ - public function it_returns_an_empty_array_if_project_does_not_contain_models() - { - $project = $this->createProjectFromClasses([]); - - $metric = new ModelsMassAssignment($project); - - $this->assertEquals([], $metric->value()); - } - - /** @test */ - public function it_returns_the_number_of_guarded_and_unguarded_models_in_the_return_array() - { - $project = $this->createProjectFromClasses([ - User::class, - Post::class, - Group::class - ]); - - $metric = new ModelsMassAssignment($project); - - $this->assertEquals([ - 'guarded' => 1, - 'fillable' => 1, - ], $metric->value()); - } -} diff --git a/tests/ShareableMetrics/Metrics/NumberOfRoutesTest.php b/tests/ShareableMetrics/Metrics/NumberOfRoutesTest.php deleted file mode 100644 index 88a042c8..00000000 --- a/tests/ShareableMetrics/Metrics/NumberOfRoutesTest.php +++ /dev/null @@ -1,35 +0,0 @@ -createProjectFromClasses([]); - - $metric = new NumberOfRoutes($project); - - $this->assertEquals('routes', $metric->name()); - } - - /** @test */ - public function it_returns_correct_number_of_routes_for_the_project() - { - Route::get('users', 'Wnx\LaravelStats\Tests\Stubs\Controllers\UsersController@index'); - Route::get('users/create', 'Wnx\LaravelStats\Tests\Stubs\Controllers\UsersController@create'); - Route::post('users', 'Wnx\LaravelStats\Tests\Stubs\Controllers\UsersController@store'); - Route::get('users/{user}', 'Wnx\LaravelStats\Tests\Stubs\Controllers\UsersController@show'); - - $project = $this->createProjectFromClasses([]); - - $metric = new NumberOfRoutes($project); - - $this->assertEquals(4, $metric->value()); - } -} diff --git a/tests/ShareableMetrics/Metrics/ProjectLinesOfCodeTest.php b/tests/ShareableMetrics/Metrics/ProjectLinesOfCodeTest.php deleted file mode 100644 index 0f2020fe..00000000 --- a/tests/ShareableMetrics/Metrics/ProjectLinesOfCodeTest.php +++ /dev/null @@ -1,38 +0,0 @@ -createProjectFromClasses([]); - - $metric = new ProjectLinesOfCode($project); - - $this->assertEquals('loc', $metric->name()); - } - - /** @test */ - public function it_returns_the_correct_total_number_lines_of_code_for_the_project() - { - $project = $this->createProjectFromClasses([ - User::class, - UsersController::class, - DemoJob::class, - DemoUnitTest::class, - ]); - - $metric = new ProjectLinesOfCode($project); - - $this->assertEquals(139, $metric->value()); - } -} diff --git a/tests/ShareableMetrics/Metrics/ProjectLogicalLinesOfCodeTest.php b/tests/ShareableMetrics/Metrics/ProjectLogicalLinesOfCodeTest.php deleted file mode 100644 index 725bb5e7..00000000 --- a/tests/ShareableMetrics/Metrics/ProjectLogicalLinesOfCodeTest.php +++ /dev/null @@ -1,38 +0,0 @@ -createProjectFromClasses([]); - - $metric = new ProjectLogicalLinesOfCode($project); - - $this->assertEquals('lloc', $metric->name()); - } - - /** @test */ - public function it_returns_the_correct_total_number_logical_lines_of_code_for_the_project() - { - $project = $this->createProjectFromClasses([ - User::class, - UsersController::class, - DemoJob::class, - DemoUnitTest::class, - ]); - - $metric = new ProjectLogicalLinesOfCode($project); - - $this->assertEquals(13.0, $metric->value()); - } -} diff --git a/tests/ShareableMetrics/Metrics/ProjectNumberOfClassesTest.php b/tests/ShareableMetrics/Metrics/ProjectNumberOfClassesTest.php deleted file mode 100644 index 8d7b8994..00000000 --- a/tests/ShareableMetrics/Metrics/ProjectNumberOfClassesTest.php +++ /dev/null @@ -1,38 +0,0 @@ -createProjectFromClasses([]); - - $metric = new ProjectNumberOfClasses($project); - - $this->assertEquals('classes', $metric->name()); - } - - /** @test */ - public function it_returns_correct_number_of_classes_for_the_given_project() - { - $project = $this->createProjectFromClasses([ - User::class, - UsersController::class, - DemoJob::class, - DemoUnitTest::class, - ]); - - $metric = new ProjectNumberOfClasses($project); - - $this->assertEquals(4, $metric->value()); - } -} diff --git a/tests/ShareableMetrics/Metrics/ScheduledTasksTest.php b/tests/ShareableMetrics/Metrics/ScheduledTasksTest.php deleted file mode 100644 index 72e9c202..00000000 --- a/tests/ShareableMetrics/Metrics/ScheduledTasksTest.php +++ /dev/null @@ -1,45 +0,0 @@ -createProjectFromClasses([]); - - $metric = new ScheduledTasks($project); - - $this->assertEquals('scheduled_tasks', $metric->name()); - } - - /** @test */ - public function it_returns_0_if_no_scheduled_tasks_can_be_found() - { - $project = $this->createProjectFromClasses([]); - - $metric = new ScheduledTasks($project); - - $this->assertEquals(0, $metric->value()); - } - - /** @test */ - public function it_returns_correct_number_of_scheduled_tasks() - { - app(Schedule::class)->command('inspire')->hourly(); - app(Schedule::class)->call(function () { - // Don't panic! - })->daily(); - - $project = $this->createProjectFromClasses([]); - - $metric = new ScheduledTasks($project); - - $this->assertEquals(2, $metric->value()); - } -} From 52faf4bbc0bc5d4d5f9e030e11453127e0ee9ce0 Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Tue, 22 Mar 2022 19:32:29 +0100 Subject: [PATCH 2/5] Deprecate share option --- src/Console/StatsListCommand.php | 74 ++------------------------ tests/Console/StatsListCommandTest.php | 66 +---------------------- 2 files changed, 6 insertions(+), 134 deletions(-) diff --git a/src/Console/StatsListCommand.php b/src/Console/StatsListCommand.php index 33730237..ebf330d7 100644 --- a/src/Console/StatsListCommand.php +++ b/src/Console/StatsListCommand.php @@ -24,9 +24,8 @@ class StatsListCommand extends Command protected $signature = 'stats {--json : Output the statistics as JSON} {-c|--components= : Comma separated list of components which should be displayed} - {--s|share : Share project statistic with Laravel community } + {--s|share : DEPRECATED Share project statistic with Laravel community } {--name= : Name used when sharing project statistic} - {--payload : Output payload to be shared with Laravel community } {--dry-run : Do not make request to share statistic}'; /** @@ -68,7 +67,7 @@ public function handle() $this->renderOutput($project); if ($this->option('share') === true) { - $this->shareDataWithShift($project); + $this->warn('The share option has been deprecated and will be removed in a future update.'); } } @@ -90,10 +89,8 @@ private function renderOutput(Project $project) $this->getArrayOfComponentsToDisplay() ); - if ($this->option('payload') !== true) { - $this->output->text(json_encode($json)); - } - } elseif ($this->option('payload') !== true) { + $this->output->text(json_encode($json)); + } else { (new AsciiTableOutput($this->output))->render( $project, $this->option('verbose'), @@ -101,67 +98,4 @@ private function renderOutput(Project $project) ); } } - - private function shareDataWithShift(Project $project): void - { - $metrics = app(CollectMetrics::class)->collect($project); - - $defaultValueForConfirmation = $this->option('no-interaction') ?? false; - - if ($this->confirm("Do you want to share stats above from your project with the Laravel Community to stats.laravelshift.com?", $defaultValueForConfirmation)) { - $projectName = $this->getProjectName(); - - if ($projectName === null) { - $this->error("Please provide a project name."); - return; - } - - if (! Str::contains($projectName, '/')) { - $this->error("Please use the organisation/repository schema for naming your project."); - return; - } - - $payload = $metrics->toHttpPayload($projectName); - - if ($this->option('payload')) { - $this->output->text(json_encode($payload)); - } - - if ($this->option('dry-run')) { - return; - } - - $wasSuccessful = app(SendToLaravelShift::class)->send($metrics->toHttpPayload($projectName)); - - if ($this->option('payload')) { - return; - } - - if ($wasSuccessful) { - $this->info("Thanks for sharing your project statistic with the community!"); - return; - } - - $this->error("Unable to share stats. (Check logs for details)"); - } - } - - private function getProjectName(): ?string - { - if ($this->option('name')) { - return (string) $this->option('name'); - } - - if (app(ProjectName::class)->hasStoredProjectName() === false) { - $generatedProjectName = app(ProjectName::class)->determineProjectNameFromGit(); - - $projectName = $this->ask("We've determined the following name for your project: \"{$generatedProjectName}\".\n Type a new name or leave it blank to continue.", $generatedProjectName); - - app(ProjectName::class)->storeNameInRcFile($projectName); - - return $projectName; - } - - return app(ProjectName::class)->get(); - } } diff --git a/tests/Console/StatsListCommandTest.php b/tests/Console/StatsListCommandTest.php index e873300f..608bf7a8 100644 --- a/tests/Console/StatsListCommandTest.php +++ b/tests/Console/StatsListCommandTest.php @@ -122,75 +122,13 @@ public function it_only_returns_stats_for_given_components() } /** @test */ - public function it_allows_users_to_share_project_statistics_with_the_community() + public function it_outputs_warning_when_someone_uses_the_share_option() { $this->artisan('stats', [ '--share' => true, - '--no-interaction' => true, - '--name' => 'repo/org', ]); $output = Artisan::output(); - $this->assertStringContainsString('Thanks for sharing your project statistic with the community!', $output); - } - - /** @test */ - public function it_uses_generated_project_name_when_sharing_project_statistics() - { - $this->artisan('stats', [ - '--share' => true, - '--no-interaction' => true, - '--payload' => true, - ]); - $output = Artisan::output(); - - $this->assertStringContainsString('\/laravel-stats', $output); - } - - /** @test */ - public function it_shows_error_message_when_project_name_does_not_follow_org_repo_schema_when_sharing() - { - $this->artisan('stats', [ - '--share' => true, - '--no-interaction' => true, - '--name' => 'foo', - ]); - $output = Artisan::output(); - - $this->assertStringContainsString('Please use the organisation/repository schema for naming your project.', $output); - } - - /** @test */ - public function it_does_not_show_success_message_for_share_option_if_dry_run_option_is_passed() - { - $this->artisan('stats', [ - '--share' => true, - '--no-interaction' => true, - '--dry-run' => true, - '--name' => 'repo/org', - ]); - $output = Artisan::output(); - - $this->assertStringNotContainsString('Thanks for sharing your project statistic with the community!', $output); - } - - /** @test */ - public function it_output_payload_to_be_sent_to_shift() - { - $this->artisan('stats', [ - '--share' => true, - '--no-interaction' => true, - '--payload' => true, - '--name' => 'repo/org', - ]); - $output = Artisan::output(); - - $output = json_decode(trim($output), true); - - $this->assertIsArray($output); - $this->assertArrayHasKey('project', $output); - $this->assertArrayHasKey('metrics', $output); - - $this->assertEquals('repo/org', $output['project']); + $this->assertStringContainsString('The share option has been deprecated and will be removed in a future update.', $output); } } From e9a611d306ea3a61f22e80fc7f7032aab40d2b6e Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Tue, 22 Mar 2022 19:35:11 +0100 Subject: [PATCH 3/5] Remove References to --share option from README --- README.md | 62 ------------------------------------------------------- 1 file changed, 62 deletions(-) diff --git a/README.md b/README.md index df8e236e..405d7eba 100644 --- a/README.md +++ b/README.md @@ -159,68 +159,6 @@ class RepositoryClassifier implements Classifier ... ``` -## Share Metrics with the Laravel Community - -You can optionally share your projects statistic by using the `--share` option. - -```shell -php artisan stats --share -``` - -Your project statistics is shared anonymously with [stats.laravelshift.com](https://stats.laravelshift.com). In regular intervals the dashboard and charts on the site are updated with shared data from other Laravel projects. - -To learn more about this feature, please check out PR [#178](https://github.com/stefanzweifel/laravel-stats/pull/178). - -### Share statistic through CI - -If you would like to share your project statistic in a CI environment you can use the `--no-interaction` and `--name`-options. - -Use the following command in your CI script to share your project statistic automatically. (Update `org/repo` with the name of your application (eg. `acme/podcasting-app`)) - -```shell -php artisan stats --share --no-interaction --name=org/repo -``` - -If you're code is hosted on GitHub, you can integrate `stats` with [GitHub Actions](https://docs.github.com/en/actions). -Copy the following Workflow to `.github/workflows/laravel-stats.yml`. It will share data when a commit is pushed to the `master` branch. The Action automatically uses your GitHub repository name in the `--name`-option. - -```yaml -name: stats - -on: - push: - branches: - - master - -jobs: - stats: - runs-on: ubuntu-latest - - steps: - - name: Checkout code - uses: actions/checkout@v2 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: 7.4 - tools: composer:v2 - - - name: Install dependencies - run: composer install --prefer-dist --no-interaction --no-suggest - - - name: Share Stats - run: php artisan stats --share --name=$GITHUB_REPOSITORY --no-interaction -``` - -### Inspect Data shared with the Community - -If you would like to inspect the payload the command is sending to the API you can use the `--dry-run` and `--payload` options. - -```shell -php artisan stats --share --no-interaction --name="org/repo" --dry-run --payload -``` - ## Treeware You're free to use this package, but if it makes it to your production environment you are required to buy the world a tree. From 2d09b78fdde78bc5969dc605d9732483a4d789c7 Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Tue, 22 Mar 2022 19:41:54 +0100 Subject: [PATCH 4/5] Remove unused Contract --- src/Contracts/CollectableMetric.php | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 src/Contracts/CollectableMetric.php diff --git a/src/Contracts/CollectableMetric.php b/src/Contracts/CollectableMetric.php deleted file mode 100644 index fa3e3c14..00000000 --- a/src/Contracts/CollectableMetric.php +++ /dev/null @@ -1,10 +0,0 @@ - Date: Tue, 22 Mar 2022 18:46:12 +0000 Subject: [PATCH 5/5] Apply php-cs-fixer changes --- src/Console/StatsListCommand.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Console/StatsListCommand.php b/src/Console/StatsListCommand.php index ebf330d7..bb5d1d65 100644 --- a/src/Console/StatsListCommand.php +++ b/src/Console/StatsListCommand.php @@ -10,9 +10,6 @@ use Wnx\LaravelStats\Project; use Wnx\LaravelStats\ReflectionClass; use Wnx\LaravelStats\RejectionStrategies\RejectVendorClasses; -use Wnx\LaravelStats\ShareableMetrics\CollectMetrics; -use Wnx\LaravelStats\ShareableMetrics\ProjectName; -use Wnx\LaravelStats\ShareableMetrics\SendToLaravelShift; class StatsListCommand extends Command {