File::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
  trait File {
0 ignored issues
show
Coding Style Compatibility introduced by
Each trait must be in a namespace of at least one level (a top-level vendor name)

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
3
    private $files;
4
    public function __construct() {
5
      $this->files = [
6
        "required" => [],
7
        "attached" => []
8
      ];
9
    }
10
    public function addFile( $_file ) {
11
      try {
12
      $this->isCorrectPath($_file);
13
      } catch( Exception $e) {
14
        throw new JException($e->getMessage());
15
      }
16
      $this->files["attached"][] = $_file;
17
    }
18
    public function addFileRequired( $_file ) {
19
      try {
20
        $this->isCorrectPath($_file);
21
      } catch( Exception $e) {
22
        throw new JException($e->getMessage());
23
      }
24
      $this->files["required"][] = $_file;
25
    }
26
    public function addFiles( $_files ) {
27
      if(!is_array($_files))
28
        throw new JException("Parameter must be an array.");
29
      foreach ($_files as $value)
30
        $this->addFile($value);
31
    }
32
    public function addFilesRequired( $_files ) {
33
      if(!is_array($_files))
34
        throw new JException("Parameter must be an array.");
35
      foreach ($_files as $value)
36
        $this->addFileRequired($value);
37
    }
38
    public function getFiles() {
39
      return $this->files["attached"];
40
    }
41
    public function getFilesRequired() {
42
      return $this->files["required"];
43
    }
44
    protected function isCorrectPath( $_file ) {
45
      if(!is_string($_file))
46
        throw new JException("Path must be a string.");
47
      if(!(file_exists($_file) || $this->isCorrectUrl($_file)))
48
        throw new JException("File [$_file] not found.");
49
    }
50
    protected function isCorrectUrl( $_url ) {
51
      return strpos(@get_headers($_url)[0],'200') === false ? false : true;
52
    }
53
  }
54
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
55