Passed
Push — master ( 82cce0...9ec6c5 )
by y
01:38
created

CustomField::isGlobalToWorkspace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Helix\Asana;
4
5
use Helix\Asana\Base\AbstractEntity;
6
use Helix\Asana\Base\AbstractEntity\CrudTrait;
7
use Helix\Asana\Base\AbstractEntity\WorkspaceTrait;
8
use Helix\Asana\CustomField\EnumOption;
9
use Helix\Asana\Task\FieldValues;
10
11
/**
12
 * A custom field.
13
 *
14
 * Use {@see FieldValues} to map {@see Task} `custom_fields` as an array-object representation of values.
15
 *
16
 * @see https://developers.asana.com/docs/asana-custom-fields
17
 * @see https://developers.asana.com/docs/custom-field
18
 *
19
 * @method string       getDescription      ()
20
 * @method $this        setDescription      (string $text)
21
 * @method EnumOption[] getEnumOptions      ()
22
 * @method string       getName             ()
23
 * @method $this        setName             (string $name)
24
 * @method int          getPrecision        ()
25
 * @method $this        setPrecision        (int $precision)
26
 * @method string       getResourceSubtype  ()
27
 * @method $this        setResourceSubtype  (string $type) @depends create-only
28
 */
29
class CustomField extends AbstractEntity {
30
31
    use CrudTrait;
32
    use WorkspaceTrait;
33
34
    const TYPE = 'custom_field';
35
    const TYPE_ENUM = 'enum';
36
    const TYPE_NUMBER = 'number';
37
    const TYPE_TEXT = 'text';
38
39
    protected const MAP = [
40
        'enum_options' => [EnumOption::class],
41
    ];
42
43
    final public function __toString (): string {
44
        return "custom_fields/{$this->getGid()}";
45
    }
46
47
    final protected function _getDir (): string {
48
        return 'custom_fields';
49
    }
50
51
    protected function _setData (array $data): void {
52
        // deprecated for resource_subtype. also removing to conceptually separate from FieldValue
53
        unset($data['type']);
54
        parent::_setData($data);
55
    }
56
57
    /**
58
     * `POST` or stage a new enum option.
59
     *
60
     * @see https://developers.asana.com/docs/create-an-enum-option
61
     *
62
     * @param string $name
63
     * @return EnumOption
64
     */
65
    public function addEnumOption (string $name) {
66
        /** @var EnumOption $option */
67
        $option = $this->_factory(EnumOption::class);
68
        $option->setName($name);
1 ignored issue
show
Unused Code introduced by
The call to Helix\Asana\CustomField\EnumOption::setName() has too many arguments starting with $name. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
        $option->/** @scrutinizer ignore-call */ 
69
                 setName($name);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
69
        if ($this->hasGid()) {
70
            $option->create();
71
        }
72
        $this->_addWithPost("{$this}/enum_options", [
73
            'name' => $name
74
        ], 'enum_options', [$option]);
75
        return $option;
76
    }
77
78
    /**
79
     * @return bool
80
     */
81
    public function hasNotificationsEnabled (): bool {
82
        return $this->_is('has_notifications_enabled');
83
    }
84
85
    /**
86
     * @return bool
87
     */
88
    final public function isEnum (): bool {
89
        return $this->getResourceSubtype() === self::TYPE_ENUM;
90
    }
91
92
    /**
93
     * @return bool
94
     */
95
    public function isGlobalToWorkspace (): bool {
96
        return $this->_is('is_global_to_workspace');
97
    }
98
99
    /**
100
     * @return bool
101
     */
102
    final public function isNumber (): bool {
103
        return $this->getResourceSubtype() === self::TYPE_NUMBER;
104
    }
105
106
    /**
107
     * @return bool
108
     */
109
    final public function isText (): bool {
110
        return $this->getResourceSubtype() === self::TYPE_TEXT;
111
    }
112
113
    /**
114
     * @param bool $flag
115
     * @return $this
116
     */
117
    public function setGlobalToWorkspace (bool $flag) {
118
        return $this->_set('is_global_to_workspace', $flag);
119
    }
120
121
    /**
122
     * @param bool $flag
123
     * @return $this
124
     */
125
    public function setNotificationsEnabled (bool $flag) {
126
        return $this->_set('has_notifications_enabled', $flag);
127
    }
128
129
    /**
130
     * @param callable $cmp `fn( EnumOption $a, EnumOption $b ): int`
131
     * @return $this
132
     */
133
    public function sortEnumOptions (callable $cmp) {
134
        if ($options = $this->getEnumOptions()) {
135
            $prev = $options[0]; // first option on remote
136
            usort($options, $cmp);
137
            if ($this->hasGid()) {
138
                foreach ($options as $option) {
139
                    if ($option !== $prev) {
140
                        $this->api->put($option, [
141
                            'insert_after' => $prev->getGid()
142
                        ]);
143
                    }
144
                    $prev = $option;
145
                }
146
            }
147
            $this->data['enum_options'] = $options;
148
            // no diff    
149
        }
150
        return $this;
151
    }
152
153
}