Skip to content

Commit

Permalink
PHPStan + cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Aug 31, 2021
1 parent c017d4d commit 5d47376
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 19 deletions.
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@
"craftcms/cms": "^3.6.0",
"league/fractal": "^0.18.0"
},
"require-dev": {
"phpstan/phpstan": "^0.12.96"
},
"autoload": {
"psr-4": {
"craft\\elementapi\\": "src/"
}
},
"scripts": {
"phpstan": "phpstan --memory-limit=1G"
},
"extra": {
"name": "Element API",
"handle": "element-api",
Expand Down
69 changes: 67 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
parameters:
level: 4
paths:
- src
scanFiles:
- vendor/craftcms/cms/src/Craft.php
- vendor/yiisoft/yii2/Yii.php
stubFiles:
- stubs/BaseYii.stub
earlyTerminatingMethodCalls:
Craft:
- dd
yii\base\Application:
- end
yii\base\ErrorHandler:
- convertExceptionToError
2 changes: 1 addition & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function registerUrlRules(RegisterUrlRulesEvent $event)
/**
* Creates a Fractal resource based on the given config.
*
* @param array|ResourceInterface|ResourceAdapterInterface
* @param array|ResourceInterface|ResourceAdapterInterface $config
* @return ResourceInterface
*/
public function createResource($config): ResourceInterface
Expand Down
37 changes: 21 additions & 16 deletions src/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ class DefaultController extends Controller
*/
public function actionIndex(string $pattern): Response
{
/** @var Response $response */
$response = Craft::$app->getResponse();
$callback = null;
$jsonOptions = null;
$pretty = false;
/** @var mixed $cache */
$cache = true;
$statusCode = 200;
$statusText = null;
Expand All @@ -71,6 +70,7 @@ public function actionIndex(string $pattern): Response
$siteId = Craft::$app->getSites()->getCurrentSite()->id;

if (is_callable($config)) {
/** @phpstan-ignore-next-line */
$params = Craft::$app->getUrlManager()->getRouteParams();
$config = $this->_callWithParams($config, $params);
}
Expand All @@ -89,12 +89,12 @@ public function actionIndex(string $pattern): Response

if (($cachedContent = $cacheService->get($cacheKey)) !== false) {
// Set the JSON headers
(new JsonResponseFormatter())->format($response);
(new JsonResponseFormatter())->format($this->response);

// Set the cached JSON on the response and return
$response->format = Response::FORMAT_RAW;
$response->content = $cachedContent;
return $response;
$this->response->format = Response::FORMAT_RAW;
$this->response->content = $cachedContent;
return $this->response;
}

$elementsService = Craft::$app->getElements();
Expand Down Expand Up @@ -179,35 +179,40 @@ public function actionIndex(string $pattern): Response

// Manually format the response ahead of time, so we can access and cache the JSON
if ($callback !== null) {
$response->data = [
$this->response->data = [
'data' => $data,
'callback' => $callback,
];
} else {
$response->data = $data;
$this->response->data = $data;
}

$formatter->format($response);
$response->data = null;
$response->format = Response::FORMAT_RAW;
$formatter->format($this->response);
$this->response->data = null;
$this->response->format = Response::FORMAT_RAW;

// Cache it?
if ($cache && $statusCode === 200) {
if ($statusCode !== 200) {
$cache = false;
}
if ($cache) {
if ($cache !== true) {
$expire = ConfigHelper::durationInSeconds($cache);
} else {
$expire = null;
}

/** @phpstan-ignore-next-line */
$dep = $elementsService->stopCollectingCacheTags();
$dep->tags[] = 'element-api';
$cacheService->set($cacheKey, $response->content, $expire, $dep);
/** @phpstan-ignore-next-line */
$cacheService->set($cacheKey, $this->response->content, $expire, $dep);
}

// Don't double-encode the data
$response->format = Response::FORMAT_RAW;
$response->setStatusCode($statusCode, $statusText);
return $response;
$this->response->format = Response::FORMAT_RAW;
$this->response->setStatusCode($statusCode, $statusText);
return $this->response;
}

/**
Expand Down
19 changes: 19 additions & 0 deletions stubs/BaseYii.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace yii;

class BaseYii
{
/**
* @var \craft\web\Application|\craft\console\Application
*/
public static $app;
}

namespace craft\console;

class Application {}

namespace craft\web;

class Application {}

0 comments on commit 5d47376

Please sign in to comment.