Passed
Push — master ( 2c7b25...f52af3 )
by Mihail
05:14
created

EntityNotificationsList::make()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 13
c 1
b 0
f 0
nc 6
nop 0
dl 0
loc 26
rs 8.439
1
<?php
2
3
namespace Apps\Model\Front\Profile;
4
5
6
use Apps\ActiveRecord\UserNotification;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Arch\Model;
9
use Ffcms\Core\Helper\Date;
10
use Ffcms\Core\Helper\Serialize;
11
use Ffcms\Core\Helper\Type\Str;
12
use Ffcms\Core\Helper\Url;
13
14
/**
15
 * Class EntityNotificationsList. Build notification messages from active record object
16
 * @package Apps\Model\Front\Profile
17
 */
18
class EntityNotificationsList extends Model
19
{
20
    public $items;
21
22
    /** @var UserNotification */
23
    private $_records;
24
25
    /**
26
     * EntityNotificationsList constructor. Pass records object inside.
27
     * @param bool $records
28
     */
29
    public function __construct($records)
30
    {
31
        $this->_records = $records;
0 ignored issues
show
Documentation Bug introduced by
It seems like $records of type boolean is incompatible with the declared type object<Apps\ActiveRecord\UserNotification> of property $_records.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32
        parent::__construct();
33
    }
34
35
    /**
36
     * Build notification list as array
37
     */
38
    public function make()
39
    {
40
        // check if records is not empty
41
        if ($this->_records === null) {
42
            return;
43
        }
44
45
        // list records and build response
46
        foreach ($this->_records as $record) {
0 ignored issues
show
Bug introduced by
The expression $this->_records of type object<Apps\ActiveRecord\UserNotification> is not traversable.
Loading history...
47
            /** @var UserNotification $record */
48
            $vars = null;
49
            if (!Str::likeEmpty($record->vars)) {
50
                $vars = Serialize::decode($record->vars);
51
            }
52
            if (!$vars !== null && isset($vars['snippet'])) {
53
                $vars['snippet'] = Url::standaloneLink($vars['snippet'], $record->uri, App::$Request->getLanguage());
0 ignored issues
show
Bug introduced by
The method standaloneLink() does not seem to exist on object<Ffcms\Core\Helper\Url>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
            }
55
            
56
            $text = App::$Translate->get('Profile', $record->msg, $vars);
0 ignored issues
show
Bug introduced by
It seems like $vars defined by \Ffcms\Core\Helper\Seria...::decode($record->vars) on line 50 can also be of type string; however, Ffcms\Core\I18n\Translate::get() does only seem to accept null|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...
57
58
            $this->items[] = [
59
                'text' => $text,
60
                'date' => Date::humanize($record->created_at)
61
            ];
62
        }
63
    }
64
65
}