Failed Conditions
Push — master ( 81ead6...837ea5 )
by
unknown
13:08
created

ApiRequest::removeParam()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace FacebookAds;
4
5
use FacebookAds\Cursor;
6
use FacebookAds\AbstractObject;
7
8
class ApiRequest {
9
  protected $api;
10
  protected $id;
11
  protected $method;
12
  protected $endpoint;
13
  protected $return_prototype;
0 ignored issues
show
Coding Style introduced by
$return_prototype does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
14
  protected $accepted_fields;
0 ignored issues
show
Coding Style introduced by
$accepted_fields does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
15
  protected $param_checker;
0 ignored issues
show
Coding Style introduced by
$param_checker does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
16
  protected $api_type;
0 ignored issues
show
Coding Style introduced by
$api_type does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
17
  protected $use_graph_video_endpoint;
0 ignored issues
show
Coding Style introduced by
$use_graph_video_endpoint does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
18
  private $fields;
19
  private $params;
20
  private $file_params;
0 ignored issues
show
Coding Style introduced by
$file_params does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
21
  private $allow_file_upload;
0 ignored issues
show
Coding Style introduced by
$allow_file_upload does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
22
  private $file_counter;
0 ignored issues
show
Coding Style introduced by
$file_counter does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
23
24
  public function __construct(
25
    Api $api,
26
    $id,
27
    $method,
28
    $endpoint,
29
    $return_prototype = null,
30
    $api_type = null,
31
    $accepted_fields = array(),
32
    TypeChecker $param_checker = null,
33
    $allow_file_upload = false,
34
    $use_graph_video_endpoint = false) {
35
    $this->fields = [];
36
    $this->params = [];
37
    $this->file_params = [];
38
    $this->file_counter = 0;
39
    $this->api = $api;
40
    $this->id = $id;
41
    $this->method = $method;
42
    $this->endpoint = $endpoint;
43
    $this->return_prototype = $return_prototype;
44
    $this->api_type = $api_type;
45
    $this->accepted_fields = $accepted_fields;
46
    $this->param_checker = $param_checker;
47
    $this->allow_file_upload = $allow_file_upload;
48
    $this->use_graph_video_endpoint = $use_graph_video_endpoint;
49
  }
50
51
  public function addParam($param, $value) {
52
    $extracted_value = $this->extractValue($value);
53
    if (!ApiConfig::TYPE_CHECKER_STRICT_MODE
54
      || !$this->param_checker->isValidParam($param)
55
    ) {
56
      $this->params[$param] = $extracted_value;
57
    } else {
58
      if ($this->param_checker->isValidParamPair($param, $value)) {
59
        if ($this->param_checker->isFileParam($param)) {
60
          $this->file_params[$param] = $extracted_value;
61
        } else {
62
          $this->params[$param] = $extracted_value;
63
        }
64
      } elseif ($this->param_checker->isPrimitiveType($param)) {
65
        $param_type = $this->param_checker->getType($param);
66
        $this->params[$param] = $this->param_checker->convertStringToPrimType(
67
          $param_type, $value);
68
      } else {
69
        throw new \LogicException('The value for '.$param.' is not compatible');
70
      }
71
    }
72
    return $this;
73
  }
74
75
  public function addParams($params) {
76
    foreach ($params as $key => $value) {
77
      $this->addParam($key, $value);
78
    }
79
    return $this;
80
  }
81
82
  public function removeParam($param) {
83
    if (array_key_exists($param, $this->params)) {
84
      unset($this->params[$param]);
85
    } elseif (array_key_exists($param, $this->params)) {
86
      unset($this->file_params[$param]);
87
    }
88
    return $this;
89
  }
90
91
  public function clearParams() {
92
    $this->params = [];
93
    $this->file_params = [];
94
    return this;
95
  }
96
97
  public function getParams() {
98
    $all_params = array_merge($this->params, $this->file_params);
99
    return $all_params;
100
  }
101
102
  public function addField($field) {
103
    if (ApiConfig::TYPE_CHECKER_STRICT_MODE
104
      && !in_array($field, $this->accepted_fields)
105
    ) {
106
      throw new \LogicException('Field '.$field.' is not supported');
107
    }
108
    if (!(in_array($field, $this->fields))) {
109
      $this->fields[] = $field;
110
    }
111
    return $this;
112
  }
113
114
  public function addFields($fields) {
115
    foreach ($fields as $field) {
116
      $this->addField($field);
117
    }
118
    return $this;
119
  }
120
121
  public function removeField($field) {
122
    if (in_array($field, $this->fields)) {
123
      $index_to_remove = array_search($field, $this->fields);
124
      unset($this->fields[$index_to_remove]);
125
    }
126
    return $this;
127
  }
128
129
  public function clearFields() {
130
    $this->fields = [];
131
    return this;
132
  }
133
134
  public function getFields() {
135
    return $this->fields;
136
  }
137
138
  public function addFile($filename) {
139
    if (ApiConfig::TYPE_CHECKER_STRICT_MODE && !$this->allow_file_upload) {
140
      throw new \LogicException("This api cannot upload files");
141
    }
142
    $file_key = 'source'.$this->file_counter;
143
    if (file_exists($filename)) {
144
      $this->file_params[$file_key] = $filename;
145
      $this->file_counter++;
146
    }
147
    return $this;
148
  }
149
150
  /**
151
   * Execute the request
152
   */
153
  public function execute() {
154
    $url_path = '/'.$this->id.$this->endpoint;
155
    $updated_params = $this->params;
156
    if (!empty($this->fields)) {
157
      $fields = implode(',', $this->fields);
158
      $updated_params['fields'] = $fields;
159
    }
160
    $response = $this->api->call(
161
      $url_path, $this->method, $updated_params, $this->file_params);
0 ignored issues
show
Unused Code introduced by
The call to Api::call() has too many arguments starting with $this->file_params.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
162
    if ($this->api_type === "EDGE" && $this->method === "GET") {
163
      return new Cursor($response, $this->return_prototype, $this->api);
164
    } else if ($this->method === "DELETE") {
165
      return $response;
166
    } else {
167
      return $this->createObject($response->getContent());
168
    }
169
  }
170
171
  protected function extractValue($value) {
172
    if ($this->param_checker->isAbstractObject($value)) {
173
      return $value->exportAllData();
174
    } else if (is_array($value)) {
175
      $extracted_value = [];
176
      foreach ($value as $key => $sub_value) {
177
        $extracted_value[$key] = $this->extractValue($sub_value);
178
      }
179
      return $extracted_value;
180
    } else {
181
      return $value;
182
    }
183
  }
184
185
  protected function createObject(array $object_data) {
186
    $object = clone $this->return_prototype;
187
    $object->setDataWithoutValidation($object_data);
188
    if ($object instanceof AbstractCrudObject) {
0 ignored issues
show
Bug introduced by
The class FacebookAds\AbstractCrudObject does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
189
      $object->setApi($this->api);
190
    }
191
    return $object;
192
  }
193
}
194