Ver Fonte

Merge pull request #357 from mpociot/analysis-zYJ1xY

Apply fixes from StyleCI
Marcel Pociot há 6 anos atrás
pai
commit
380cbb9963

+ 0 - 2
config/apidoc.php

@@ -7,7 +7,6 @@ return [
      */
     'output' => 'public/docs',
 
-
     /*
      * The router to be used (Laravel or Dingo).
      */
@@ -18,7 +17,6 @@ return [
      */
     'postman' => true,
 
-
     /*
      * The routes for which documentation should be generated.
      * Each group contains rules defining which routes should be included ('match', 'include' and 'exclude' sections)

+ 2 - 2
src/ApiDocGeneratorServiceProvider.php

@@ -15,10 +15,10 @@ class ApiDocGeneratorServiceProvider extends ServiceProvider
      */
     public function boot()
     {
-        $this->loadViewsFrom(__DIR__ . '/../resources/views/', 'apidoc');
+        $this->loadViewsFrom(__DIR__.'/../resources/views/', 'apidoc');
 
         $this->publishes([
-            __DIR__ . '/../resources/views' => app()->basePath().'/resources/views/vendor/apidoc',
+            __DIR__.'/../resources/views' => app()->basePath().'/resources/views/vendor/apidoc',
         ], 'views');
 
         $this->publishes([

+ 9 - 12
src/Commands/GenerateDocumentation.php

@@ -2,12 +2,12 @@
 
 namespace Mpociot\ApiDoc\Commands;
 
-use Mpociot\ApiDoc\Tools\RouteMatcher;
 use ReflectionClass;
 use Illuminate\Routing\Route;
 use Illuminate\Console\Command;
 use Mpociot\Reflection\DocBlock;
 use Illuminate\Support\Collection;
+use Mpociot\ApiDoc\Tools\RouteMatcher;
 use Mpociot\Documentarian\Documentarian;
 use Mpociot\ApiDoc\Postman\CollectionWriter;
 use Mpociot\ApiDoc\Generators\DingoGenerator;
@@ -32,7 +32,6 @@ class GenerateDocumentation extends Command
      */
     protected $description = 'Generate your API documentation from existing Laravel routes.';
 
-
     private $routeMatcher;
 
     public function __construct(RouteMatcher $routeMatcher)
@@ -58,12 +57,11 @@ class GenerateDocumentation extends Command
             $generator = new DingoGenerator();
         }
 
-
         $parsedRoutes = $this->processRoutes($generator, $routes);
         $parsedRoutes = collect($parsedRoutes)->groupBy('resource')
             ->sort(function ($a, $b) {
                 return strcmp($a->first()['resource'], $b->first()['resource']);
-        });
+            });
 
         $this->writeMarkdown($parsedRoutes);
     }
@@ -177,24 +175,23 @@ class GenerateDocumentation extends Command
         }
     }
 
-
     /**
      * @param AbstractGenerator $generator
      * @param array $routes
-     * @return array
      *
+     * @return array
      */
     private function processRoutes(AbstractGenerator $generator, array $routes)
     {
         $parsedRoutes = [];
         foreach ($routes as ['route' => $route, 'apply' => $apply]) {
             /** @var Route $route */
-                if ($this->isValidRoute($route) && $this->isRouteVisibleForDocumentation($route->getAction()['uses'])) {
-                    $parsedRoutes[] = $generator->processRoute($route, $apply);
-                    $this->info('Processed route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route));
-                } else {
-                    $this->warn('Skipping route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route));
-                }
+            if ($this->isValidRoute($route) && $this->isRouteVisibleForDocumentation($route->getAction()['uses'])) {
+                $parsedRoutes[] = $generator->processRoute($route, $apply);
+                $this->info('Processed route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route));
+            } else {
+                $this->warn('Skipping route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route));
+            }
         }
 
         return $parsedRoutes;

+ 1 - 0
src/Generators/AbstractGenerator.php

@@ -122,6 +122,7 @@ abstract class AbstractGenerator
 
     /**
      * @param array $routeAction
+     *
      * @return array
      */
     protected function getParametersFromDocBlock($routeAction)

+ 4 - 4
src/Tools/RouteMatcher.php

@@ -10,7 +10,7 @@ class RouteMatcher
 {
     public function getDingoRoutesToBeDocumented(array $routeRules)
     {
-        return $this->getRoutesToBeDocumented($routeRules,true);
+        return $this->getRoutesToBeDocumented($routeRules, true);
     }
 
     public function getLaravelRoutesToBeDocumented(array $routeRules)
@@ -48,11 +48,12 @@ class RouteMatcher
 
     private function getAllRoutes(bool $usingDingoRouter, array $versions = [])
     {
-        if (!$usingDingoRouter) {
+        if (! $usingDingoRouter) {
             return RouteFacade::getRoutes();
         }
 
         $allRouteCollections = app(\Dingo\Api\Routing\Router::class)->getRoutes();
+
         return collect($allRouteCollections)
             ->flatMap(function (RouteCollection $collection) {
                 return $collection->getRoutes();
@@ -62,7 +63,7 @@ class RouteMatcher
     private function shouldIncludeRoute(Route $route, array $routeRule, array $mustIncludes, bool $usingDingoRouter)
     {
         $matchesVersion = $usingDingoRouter
-            ? !empty(array_intersect($route->versions(), $routeRule['match']['versions'] ?? []))
+            ? ! empty(array_intersect($route->versions(), $routeRule['match']['versions'] ?? []))
             : true;
 
         return in_array($route->getName(), $mustIncludes)
@@ -70,5 +71,4 @@ class RouteMatcher
             && str_is($routeRule['match']['prefixes'] ?? [], $route->uri())
             && $matchesVersion);
     }
-
 }

+ 0 - 3
tests/DingoGeneratorTest.php

@@ -5,10 +5,8 @@ namespace Mpociot\ApiDoc\Tests;
 use Orchestra\Testbench\TestCase;
 use Mpociot\ApiDoc\Generators\DingoGenerator;
 use Dingo\Api\Provider\LaravelServiceProvider;
-use Mpociot\ApiDoc\Tests\Fixtures\TestRequest;
 use Mpociot\ApiDoc\Tests\Fixtures\TestController;
 use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider;
-use Mpociot\ApiDoc\Tests\Fixtures\DingoTestController;
 
 class DingoGeneratorTest extends TestCase
 {
@@ -74,5 +72,4 @@ class DingoGeneratorTest extends TestCase
         $parsed = $this->generator->processRoute($route);
         $this->assertSame(['DELETE'], $parsed['methods']);
     }
-
 }

+ 1 - 1
tests/GenerateDocumentationTest.php

@@ -34,7 +34,7 @@ class GenerateDocumentationTest extends TestCase
     public function tearDown()
     {
         // delete the generated docs - compatible cross-platform
-        $dir = __DIR__.'/../public/docs';/*
+        $dir = __DIR__.'/../public/docs'; /*
         if (is_dir($dir)) {
             $files = new RecursiveIteratorIterator(
                 new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),

+ 103 - 44
tests/RouteMatcherTest.php

@@ -43,14 +43,14 @@ class RouteMatcherTest extends TestCase
         $routeRules[0]['match']['domains'] = ['domain1.*'];
         $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
         $this->assertCount(6, $routes);
-        foreach ($routes as $route){
+        foreach ($routes as $route) {
             $this->assertContains('domain1', $route['route']->getDomain());
         }
 
         $routeRules[0]['match']['domains'] = ['domain2.*'];
         $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
         $this->assertCount(6, $routes);
-        foreach ($routes as $route){
+        foreach ($routes as $route) {
             $this->assertContains('domain2', $route['route']->getDomain());
         }
     }
@@ -72,14 +72,14 @@ class RouteMatcherTest extends TestCase
         $routeRules[0]['match']['domains'] = ['domain1.*'];
         $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
         $this->assertCount(6, $routes);
-        foreach ($routes as $route){
+        foreach ($routes as $route) {
             $this->assertContains('domain1', $route['route']->getDomain());
         }
 
         $routeRules[0]['match']['domains'] = ['domain2.*'];
         $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
         $this->assertCount(6, $routes);
-        foreach ($routes as $route){
+        foreach ($routes as $route) {
             $this->assertContains('domain2', $route['route']->getDomain());
         }
     }
@@ -100,14 +100,14 @@ class RouteMatcherTest extends TestCase
         $routeRules[0]['match']['prefixes'] = ['prefix1/*'];
         $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
         $this->assertCount(4, $routes);
-        foreach ($routes as $route){
+        foreach ($routes as $route) {
             $this->assertTrue(str_is('prefix1/*', $route['route']->uri()));
         }
 
         $routeRules[0]['match']['prefixes'] = ['prefix2/*'];
         $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
         $this->assertCount(4, $routes);
-        foreach ($routes as $route){
+        foreach ($routes as $route) {
             $this->assertTrue(str_is('prefix2/*', $route['route']->uri()));
         }
     }
@@ -129,14 +129,14 @@ class RouteMatcherTest extends TestCase
         $routeRules[0]['match']['prefixes'] = ['prefix1/*'];
         $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
         $this->assertCount(4, $routes);
-        foreach ($routes as $route){
+        foreach ($routes as $route) {
             $this->assertTrue(str_is('prefix1/*', $route['route']->uri()));
         }
 
         $routeRules[0]['match']['prefixes'] = ['prefix2/*'];
         $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
         $this->assertCount(4, $routes);
-        foreach ($routes as $route){
+        foreach ($routes as $route) {
             $this->assertTrue(str_is('prefix2/*', $route['route']->uri()));
         }
     }
@@ -150,7 +150,7 @@ class RouteMatcherTest extends TestCase
         $routeRules[0]['match']['prefixes'] = ['*'];
         $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
         $this->assertCount(6, $routes);
-        foreach ($routes as $route){
+        foreach ($routes as $route) {
             $this->assertNotEmpty(array_intersect($route['route']->versions(), ['v2']));
         }
 
@@ -186,7 +186,7 @@ class RouteMatcherTest extends TestCase
                 'match' => [
                     'domains' => ['domain1.*'],
                     'prefixes' => ['prefix1/*'],
-                    'versions' => ['v1']
+                    'versions' => ['v1'],
                 ],
                 'include' => [$mustInclude],
             ],
@@ -223,7 +223,7 @@ class RouteMatcherTest extends TestCase
                 'match' => [
                     'domains' => ['domain2.*'],
                     'prefixes' => ['*'],
-                    'versions' => ['v2']
+                    'versions' => ['v2'],
                 ],
                 'exclude' => [$mustNotInclude],
             ],
@@ -296,12 +296,12 @@ class RouteMatcherTest extends TestCase
 
         $routes = collect($routes);
         $firstRuleGroup = $routes->filter(function ($route) {
-            return !empty(array_intersect($route['route']->versions(), ['v1']));
+            return ! empty(array_intersect($route['route']->versions(), ['v1']));
         });
         $this->assertCount(12, $firstRuleGroup);
 
         $secondRuleGroup = $routes->filter(function ($route) {
-            return !empty(array_intersect($route['route']->versions(), ['v2']));
+            return ! empty(array_intersect($route['route']->versions(), ['v2']));
         });
         $this->assertCount(6, $secondRuleGroup);
     }
@@ -309,55 +309,114 @@ class RouteMatcherTest extends TestCase
     private function registerLaravelRoutes()
     {
         RouteFacade::group(['domain' => 'domain1.app.test'], function () {
-            RouteFacade::post('/domain1-1', function () { return 'hi'; })->name('domain1-1');
-            RouteFacade::get('domain1-2', function () { return 'hi'; })->name('domain1-2');
-            RouteFacade::get('/prefix1/domain1-1', function () { return 'hi'; })->name('prefix1.domain1-1');
-            RouteFacade::get('prefix1/domain1-2', function () { return 'hi'; })->name('prefix1.domain1-2');
-            RouteFacade::get('/prefix2/domain1-1', function () { return 'hi'; })->name('prefix2.domain1-1');
-            RouteFacade::get('prefix2/domain1-2', function () { return 'hi'; })->name('prefix2.domain1-2');
+            RouteFacade::post('/domain1-1', function () {
+                return 'hi';
+            })->name('domain1-1');
+            RouteFacade::get('domain1-2', function () {
+                return 'hi';
+            })->name('domain1-2');
+            RouteFacade::get('/prefix1/domain1-1', function () {
+                return 'hi';
+            })->name('prefix1.domain1-1');
+            RouteFacade::get('prefix1/domain1-2', function () {
+                return 'hi';
+            })->name('prefix1.domain1-2');
+            RouteFacade::get('/prefix2/domain1-1', function () {
+                return 'hi';
+            })->name('prefix2.domain1-1');
+            RouteFacade::get('prefix2/domain1-2', function () {
+                return 'hi';
+            })->name('prefix2.domain1-2');
         });
         RouteFacade::group(['domain' => 'domain2.app.test'], function () {
-            RouteFacade::post('/domain2-1', function () { return 'hi'; })->name('domain2-1');
-            RouteFacade::get('domain2-2', function () { return 'hi'; })->name('domain2-2');
-            RouteFacade::get('/prefix1/domain2-1', function () { return 'hi'; })->name('prefix1.domain2-1');
-            RouteFacade::get('prefix1/domain2-2', function () { return 'hi'; })->name('prefix1.domain2-2');
-            RouteFacade::get('/prefix2/domain2-1', function () { return 'hi'; })->name('prefix2.domain2-1');
-            RouteFacade::get('prefix2/domain2-2', function () { return 'hi'; })->name('prefix2.domain2-2');
+            RouteFacade::post('/domain2-1', function () {
+                return 'hi';
+            })->name('domain2-1');
+            RouteFacade::get('domain2-2', function () {
+                return 'hi';
+            })->name('domain2-2');
+            RouteFacade::get('/prefix1/domain2-1', function () {
+                return 'hi';
+            })->name('prefix1.domain2-1');
+            RouteFacade::get('prefix1/domain2-2', function () {
+                return 'hi';
+            })->name('prefix1.domain2-2');
+            RouteFacade::get('/prefix2/domain2-1', function () {
+                return 'hi';
+            })->name('prefix2.domain2-1');
+            RouteFacade::get('prefix2/domain2-2', function () {
+                return 'hi';
+            })->name('prefix2.domain2-2');
         });
     }
 
     private function registerDingoRoutes()
     {
-
         $api = app('api.router');
         $api->version('v1', function (Router $api) {
             $api->group(['domain' => 'domain1.app.test'], function (Router $api) {
-                $api->post('/domain1-1', function () { return 'hi'; })->name('v1.domain1-1');
-                $api->get('domain1-2', function () { return 'hi'; })->name('v1.domain1-2');
-                $api->get('/prefix1/domain1-1', function () { return 'hi'; })->name('v1.prefix1.domain1-1');
-                $api->get('prefix1/domain1-2', function () { return 'hi'; })->name('v1.prefix1.domain1-2');
-                $api->get('/prefix2/domain1-1', function () { return 'hi'; })->name('v1.prefix2.domain1-1');
-                $api->get('prefix2/domain1-2', function () { return 'hi'; })->name('v1.prefix2.domain1-2');
+                $api->post('/domain1-1', function () {
+                    return 'hi';
+                })->name('v1.domain1-1');
+                $api->get('domain1-2', function () {
+                    return 'hi';
+                })->name('v1.domain1-2');
+                $api->get('/prefix1/domain1-1', function () {
+                    return 'hi';
+                })->name('v1.prefix1.domain1-1');
+                $api->get('prefix1/domain1-2', function () {
+                    return 'hi';
+                })->name('v1.prefix1.domain1-2');
+                $api->get('/prefix2/domain1-1', function () {
+                    return 'hi';
+                })->name('v1.prefix2.domain1-1');
+                $api->get('prefix2/domain1-2', function () {
+                    return 'hi';
+                })->name('v1.prefix2.domain1-2');
             });
             $api->group(['domain' => 'domain2.app.test'], function (Router $api) {
-                $api->post('/domain2-1', function () { return 'hi'; })->name('v1.domain2-1');
-                $api->get('domain2-2', function () { return 'hi'; })->name('v1.domain2-2');
-                $api->get('/prefix1/domain2-1', function () { return 'hi'; })->name('v1.prefix1.domain2-1');
-                $api->get('prefix1/domain2-2', function () { return 'hi'; })->name('v1.prefix1.domain2-2');
-                $api->get('/prefix2/domain2-1', function () { return 'hi'; })->name('v1.prefix2.domain2-1');
-                $api->get('prefix2/domain2-2', function () { return 'hi'; })->name('v1.prefix2.domain2-2');
+                $api->post('/domain2-1', function () {
+                    return 'hi';
+                })->name('v1.domain2-1');
+                $api->get('domain2-2', function () {
+                    return 'hi';
+                })->name('v1.domain2-2');
+                $api->get('/prefix1/domain2-1', function () {
+                    return 'hi';
+                })->name('v1.prefix1.domain2-1');
+                $api->get('prefix1/domain2-2', function () {
+                    return 'hi';
+                })->name('v1.prefix1.domain2-2');
+                $api->get('/prefix2/domain2-1', function () {
+                    return 'hi';
+                })->name('v1.prefix2.domain2-1');
+                $api->get('prefix2/domain2-2', function () {
+                    return 'hi';
+                })->name('v1.prefix2.domain2-2');
             });
         });
         $api->version('v2', function (Router $api) {
             $api->group(['domain' => 'domain1.app.test'], function (Router $api) {
-                $api->post('/domain1', function () { return 'hi'; })->name('v2.domain1');
-                $api->get('/prefix1/domain1', function () { return 'hi'; })->name('v2.prefix1.domain1');
-                $api->get('/prefix2/domain1', function () { return 'hi'; })->name('v2.prefix2.domain1');
+                $api->post('/domain1', function () {
+                    return 'hi';
+                })->name('v2.domain1');
+                $api->get('/prefix1/domain1', function () {
+                    return 'hi';
+                })->name('v2.prefix1.domain1');
+                $api->get('/prefix2/domain1', function () {
+                    return 'hi';
+                })->name('v2.prefix2.domain1');
             });
             $api->group(['domain' => 'domain2.app.test'], function (Router $api) {
-                $api->post('/domain2', function () { return 'hi'; })->name('v2.domain2');
-                $api->get('/prefix1/domain2', function () { return 'hi'; })->name('v2.prefix1.domain2');
-                $api->get('/prefix2/domain2', function () { return 'hi'; })->name('v2.prefix2.domain2');
+                $api->post('/domain2', function () {
+                    return 'hi';
+                })->name('v2.domain2');
+                $api->get('/prefix1/domain2', function () {
+                    return 'hi';
+                })->name('v2.prefix1.domain2');
+                $api->get('/prefix2/domain2', function () {
+                    return 'hi';
+                })->name('v2.prefix2.domain2');
             });
         });
     }