Skip to content

Commit

Permalink
v1.7.11 Changes (#491)
Browse files Browse the repository at this point in the history
* Remove exception if no routes are found #489

* Remove unncessary instance creation in AnnotationUtility #488

* Check if Table is a subclass of Table in ModelScanner #486

* Check if class exists and is instaniable

* suppress phpmd cyclomatic complexity
  • Loading branch information
cnizzardini committed Dec 2, 2022
1 parent 5b8bd84 commit decc182
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 67 deletions.
10 changes: 0 additions & 10 deletions src/Lib/Factory/SwaggerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use Cake\Routing\Router;
use SwaggerBake\Lib\Configuration;
use SwaggerBake\Lib\Exception\SwaggerBakeRunTimeException;
use SwaggerBake\Lib\Model\ModelScanner;
use SwaggerBake\Lib\Route\RouteScanner;
use SwaggerBake\Lib\Swagger;
Expand Down Expand Up @@ -49,15 +48,6 @@ public function __construct(?Configuration $config = null, ?RouteScanner $routeS
*/
public function create(): Swagger
{
$routes = $this->routeScanner->getRoutes();

if (empty($routes)) {
throw new SwaggerBakeRunTimeException(
'No restful routes were found for your prefix `' . $this->config->getPrefix() . '`. ' .
'Try adding restful routes to your `config/routes.php`.'
);
}

return new Swagger(new ModelScanner($this->routeScanner, $this->config));
}
}
10 changes: 10 additions & 0 deletions src/Lib/Model/ModelScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Cake\Collection\Collection;
use Cake\Database\Connection;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\Table;
use MixerApi\Core\Model\Model;
use MixerApi\Core\Model\ModelFactory;
use MixerApi\Core\Utility\NamespaceUtility;
Expand Down Expand Up @@ -50,6 +51,7 @@ public function __construct(RouteScanner $routeScanner, Configuration $config)
/**
* Gets an array of ModelDecorator instances
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @return \SwaggerBake\Lib\Model\ModelDecorator[]
*/
public function getModelDecorators(): array
Expand All @@ -69,6 +71,14 @@ public function getModelDecorators(): array

foreach ($tables as $table) {
try {
if (!class_exists($table)) {
continue;
}

$reflectionClass = new \ReflectionClass($table);
if (!$reflectionClass->isInstantiable() || !$reflectionClass->isSubclassOf(Table::class)) {
continue;
}
$model = (new ModelFactory($connection, new $table()))->create();
} catch (\Exception $e) {
continue;
Expand Down
6 changes: 2 additions & 4 deletions src/Lib/Utility/AnnotationUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ class AnnotationUtility
public static function getClassAnnotationsFromFqns(string $namespace): array
{
try {
$instance = new $namespace();
$reflectionClass = new ReflectionClass(get_class($instance));
$reflectionClass = new ReflectionClass($namespace);
} catch (Exception $e) {
return [];
}
Expand Down Expand Up @@ -83,8 +82,7 @@ public static function getMethodAnnotations(string $namespace, string $method):
$return = [];

try {
$instance = new $namespace();
$reflectionClass = new ReflectionClass(get_class($instance));
$reflectionClass = new ReflectionClass($namespace);
$reflectedMethods = $reflectionClass->getMethods();
} catch (Exception $e) {
return $return;
Expand Down
53 changes: 0 additions & 53 deletions tests/TestCase/Lib/Factory/SwaggerFactoryTest.php

This file was deleted.

0 comments on commit decc182

Please sign in to comment.