for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/**
* @link http://www.stombox.com/
* @copyright Copyright (c) 2015 Stombox LLC
* @license http://www.stombox.com/license/
*/
namespace tuyakhov\jsonapi;
use yii\db\BaseActiveRecord;
use yii\rest\Action;
use yii\web\BadRequestHttpException;
use yii\web\ServerErrorHttpException;
class UpdateRelationshipAction extends Action
{
* @param $id
* @param $name
* @throws BadRequestHttpException
* @throws ServerErrorHttpException
public function run($id, $name)
/** @var BaseActiveRecord $model */
$model = $this->findModel($id);
if (!$model instanceof ResourceInterface) {
throw new BadRequestHttpException('Impossible to update relationships for resource');
}
$data = \Yii::$app->getRequest()->getBodyParams();
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
$a = "a"; $ab = "ab"; $abc = "abc";
will produce issues in the first and second line, while this second example
will produce no issues.
$relationships = [];
foreach ($data as $modelName => $identifier) {
if (!isset($identifier['id'])) {
continue;
/** @var BaseActiveRecord $modelName */
if ($relationship = $modelName::findOne($identifier['id'])) {
$relationships[] = $relationship;
if (!empty($relationships)) {
$model->unlinkAll($name);
$model->setResourceRelationship($name, $relationships);
throw new ServerErrorHttpException('Failed to update the relationship for unknown reason.');
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.