Completed
Push — master ( 7d0685...a02648 )
by Ryan
06:24
created

EntryPresenter::createdAtDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php namespace Anomaly\Streams\Platform\Entry;
2
3
use Anomaly\Streams\Platform\Entry\Contract\EntryInterface;
4
use Anomaly\Streams\Platform\Model\EloquentModel;
5
use Anomaly\Streams\Platform\Model\EloquentPresenter;
6
use Anomaly\Streams\Platform\Support\Value;
7
8
/**
9
 * Class EntryPresenter
10
 *
11
 * @link    http://pyrocms.com/
12
 * @author  PyroCMS, Inc. <[email protected]>
13
 * @author  Ryan Thompson <[email protected]>
14
 */
15
class EntryPresenter extends EloquentPresenter
16
{
17
18
    /**
19
     * The resource object.
20
     * This is for IDE hinting.
21
     *
22
     * @var EntryInterface|EloquentModel
23
     */
24
    protected $object;
25
26
    /**
27
     * Return the date string for created at.
28
     *
29
     * @return string
30
     */
31
    public function createdAtDate()
32
    {
33
        return $this->object->created_at
34
            ->setTimezone(config('app.timezone'))
35
            ->format(config('streams.date_format'));
36
    }
37
38
    /**
39
     * Return the datetime string for created at.
40
     *
41
     * @return string
42
     */
43
    public function createdAtDatetime()
44
    {
45
        return $this->object->created_at
46
            ->setTimezone(config('app.timezone'))
47
            ->format(config('streams.date_format') . ' ' . config('streams.time_format'));
48
    }
49
50
    /**
51
     * Return the date string for updated at.
52
     *
53
     * @return string
54
     */
55
    public function updatedAtDate()
56
    {
57
        return $this->object->updated_at
58
            ->setTimezone(config('app.timezone'))
59
            ->format(config('streams.date_format'));
60
    }
61
62
    /**
63
     * Return the datetime string for updated at.
64
     *
65
     * @return string
66
     */
67
    public function updatedAtDatetime()
68
    {
69
        return $this->object->updated_at
70
            ->setTimezone(config('app.timezone'))
71
            ->format(config('streams.date_format') . ' ' . config('streams.time_format'));
72
    }
73
74
    /**
75
     * Return a label.
76
     *
77
     * @param         $text
78
     * @param  string $context
79
     * @param  string $size
80
     * @return string
81
     */
82
    public function label($text = null, $context = null, $size = null)
83
    {
84
        if (!$text) {
85
            $text = $this->object->getTitleName();
86
        }
87
88
        if (!$context) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $context of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
89
            $context = 'default';
90
        }
91
92
        if (!$size) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $size of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
93
            $size = 'sm';
94
        }
95
96
        /* @var Value $value */
97
        $value = app(Value::class);
98
99
        $text = $value->make($text, $this->object);
100
101
        if (trans()->has($text) && is_string(trans($text))) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Translation\TranslatorInterface as the method has() does only exist in the following implementations of said interface: Illuminate\Translation\Translator.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
102
            $text = trans($text);
103
        }
104
105
        return '<span class="label label-' . $context . ' label-' . $size . '">' . $text . '</span>';
106
    }
107
108
    /**
109
     * Return the edit URL.
110
     *
111
     * @return string
112
     */
113 View Code Duplication
    public function editUrl()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115
        return url(
116
            implode(
117
                '/',
118
                array_unique(
119
                    array_filter(
120
                        [
121
                            'admin',
122
                            $this->object->getStreamNamespace(),
0 ignored issues
show
Bug introduced by
The method getStreamNamespace does only exist in Anomaly\Streams\Platform...Contract\EntryInterface, but not in Anomaly\Streams\Platform\Model\EloquentModel.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
123
                            $this->object->getStreamSlug(),
0 ignored issues
show
Bug introduced by
The method getStreamSlug does only exist in Anomaly\Streams\Platform...Contract\EntryInterface, but not in Anomaly\Streams\Platform\Model\EloquentModel.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
124
                            'edit',
125
                            $this->object->getId(),
126
                        ]
127
                    )
128
                )
129
            )
130
        );
131
    }
132
133
    /**
134
     * Return the edit link.
135
     *
136
     * @return string
137
     */
138
    public function editLink()
139
    {
140
        return app('html')->link($this->editUrl(), $this->object->{$this->object->getTitleName()});
141
    }
142
143
    /**
144
     * Return the view URL.
145
     *
146
     * @return string
147
     */
148 View Code Duplication
    public function viewUrl()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
    {
150
        return url(
151
            implode(
152
                '/',
153
                array_unique(
154
                    array_filter(
155
                        [
156
                            'admin',
157
                            $this->object->getStreamNamespace(),
0 ignored issues
show
Bug introduced by
The method getStreamNamespace does only exist in Anomaly\Streams\Platform...Contract\EntryInterface, but not in Anomaly\Streams\Platform\Model\EloquentModel.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
158
                            $this->object->getStreamSlug(),
0 ignored issues
show
Bug introduced by
The method getStreamSlug does only exist in Anomaly\Streams\Platform...Contract\EntryInterface, but not in Anomaly\Streams\Platform\Model\EloquentModel.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
159
                            'view',
160
                            $this->object->getId(),
161
                        ]
162
                    )
163
                )
164
            )
165
        );
166
    }
167
168
    /**
169
     * Return the view link.
170
     *
171
     * @return string
172
     */
173
    public function viewLink()
174
    {
175
        return app('html')->link($this->viewUrl(), $this->object->{$this->object->getTitleName()});
176
    }
177
178
    /**
179
     * When accessing a property of a decorated entry
180
     * object first check to see if the key represents
181
     * a streams field. If it does then return the field
182
     * type's presenter object. Otherwise handle normally.
183
     *
184
     * @param  $key
185
     * @return mixed
186
     */
187
    public function __get($key)
188
    {
189
        if ($assignment = $this->object->getAssignment($key)) {
0 ignored issues
show
Bug introduced by
The method getAssignment does only exist in Anomaly\Streams\Platform...Contract\EntryInterface, but not in Anomaly\Streams\Platform\Model\EloquentModel.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
190
            $type = $assignment->getFieldType();
191
192
            if ($assignment->isTranslatable() && $locale = config('app.locale')) {
193
                $entry = $this->object->translateOrDefault($locale);
0 ignored issues
show
Bug introduced by
The method translateOrDefault does only exist in Anomaly\Streams\Platform\Model\EloquentModel, but not in Anomaly\Streams\Platform...Contract\EntryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
194
195
                $type->setLocale($locale);
196
            } else {
197
                $entry = $this->object;
198
            }
199
200
            $type->setEntry($entry);
201
202
            if (method_exists($type, 'getRelation')) {
203
                return $type->decorate($entry->getRelationValue(camel_case($key)));
204
            }
205
206
            $type->setValue($entry->getFieldValue($key));
207
208
            return $type->getPresenter();
209
        }
210
211
        return $this->__getDecorator()->decorate(parent::__get($key));
212
    }
213
}
214