SuggestReceiverCall   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 10
c 4
b 0
f 0
lcom 0
cbo 4
dl 0
loc 78
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C createTag() 0 35 7
A getTagUid() 0 21 3
1
<?php
2
3
/**
4
 * Suggest call
5
 */
6
7
namespace HDNET\Tagger\Hooks;
8
9
use HDNET\Tagger\Service;
10
use TYPO3\CMS\Core\Database\DatabaseConnection;
11
use TYPO3\CMS\Core\Http\AjaxRequestHandler;
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
14
/**
15
 * Class SuggestReceiverCall
16
 */
17
class SuggestReceiverCall
18
{
19
20
    const TAG = 'tx_tagger_domain_model_tag';
21
    const LLPATH = 'LLL:EXT:tagger/Resources/Private/Language/locallang.xml:tag_suggest_';
22
23
    /**
24
     * Create a tag
25
     *
26
     * @param array $params
27
     * @param AjaxRequestHandler $ajaxObj
28
     * @return void
29
     */
30
    public function createTag(array $params, AjaxRequestHandler $ajaxObj)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
    {
32
        $request = GeneralUtility::_POST();
33
34
        try {
35
            // check if a tag is submitted
36
            if (!isset($request['item']) || empty($request['item'])) {
37
                throw new \Exception('error_no-tag');
38
            }
39
40
            $newsUid = $request['newsid'];
41
            if ((int)$newsUid === 0 && (strlen($newsUid) == 16 && !GeneralUtility::isFirstPartOfStr($newsUid, 'NEW'))) {
42
                throw new \Exception('error_no-newsid');
43
            }
44
45
            // get tag uid
46
            $newTagId = $this->getTagUid($request);
0 ignored issues
show
Bug introduced by
It seems like $request defined by \TYPO3\CMS\Core\Utility\GeneralUtility::_POST() on line 32 can also be of type string; however, HDNET\Tagger\Hooks\Sugge...ceiverCall::getTagUid() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
47
48
            $ajaxObj->setContentFormat('javascript');
49
            $ajaxObj->setContent('');
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
50
            $response = [
51
                $newTagId,
52
                $request['item'],
53
                'tx_tagger_domain_model_tag',
54
                self::TAG,
55
                'tags',
56
                'data[tx_tagger_domain_model_tag][' . $newsUid . '][tags]',
57
                $newsUid
58
            ];
59
            $ajaxObj->setJavascriptCallbackWrap(implode('-', $response));
60
        } catch (\Exception $e) {
61
            //$errorMsg = $GLOBALS['LANG']->sL(self::LLPATH . );
62
            $ajaxObj->setError($e->getMessage());
63
        }
64
    }
65
66
    /**
67
     * Get the uid of the tag, either bei inserting as new or get existing
68
     *
69
     * @param array $request ajax request
70
     * @return int
71
     * @throws \Exception
72
     */
73
    protected function getTagUid(array $request)
74
    {
75
        /** @var DatabaseConnection $databaseConnection */
76
        $databaseConnection = $GLOBALS['TYPO3_DB'];
77
        $record = $databaseConnection->exec_SELECTgetSingleRow(
78
            '*',
79
            'tx_tagger_domain_model_tag',
80
            'deleted=0 AND title=' . $databaseConnection->fullQuoteStr($request['item'], 'tx_tagger_domain_model_tag')
81
        );
82
        if (isset($record['uid'])) {
83
            $tagUid = (int)$record['uid'];
84
        } else {
85
            $tagUid = Service\IntegrationService::createTagRecord($request['item']);
86
        }
87
88
        if ($tagUid == 0) {
89
            throw new \Exception('error_no-tag-created');
90
        }
91
92
        return $tagUid;
93
    }
94
}
95