File   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 65
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 12 5
A addFile() 0 8 2
B isActive() 0 16 5
1
<?php
2
3
/*
4
 * janitor (http://juliangut.com/janitor).
5
 * Effortless maintenance management.
6
 *
7
 * @license BSD-3-Clause
8
 * @link https://github.com/juliangut/janitor
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
namespace Janitor\Watcher;
13
14
/**
15
 * File existence maintenance status watcher.
16
 */
17
class File implements WatcherInterface
18
{
19
    /**
20
     * Files.
21
     *
22
     * @var array
23
     */
24
    protected $files;
25
26
    /**
27
     * File constructor.
28
     *
29
     * @param string|array $files
0 ignored issues
show
Documentation introduced by
Should the type for parameter $files not be string|array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
30
     */
31
    public function __construct($files = null)
32
    {
33
        if ($files !== null && !is_array($files)) {
34
            $files = [$files];
35
        }
36
37
        if (is_array($files)) {
38
            foreach ($files as $file) {
39
                $this->addFile($file);
40
            }
41
        }
42
    }
43
44
    /**
45
     * Add file path.
46
     *
47
     * @param string $file
48
     *
49
     * @return $this
50
     */
51
    public function addFile($file)
52
    {
53
        if (trim($file) !== '') {
54
            $this->files[] = trim($file);
55
        }
56
57
        return $this;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     *
63
     * @throws \RuntimeException
64
     */
65
    public function isActive()
66
    {
67
        if (!count($this->files)) {
68
            throw new \RuntimeException('At least one file path must be defined');
69
        }
70
71
        foreach ($this->files as $file) {
72
            $file = realpath($file);
73
74
            if (file_exists($file) && is_file($file)) {
75
                return true;
76
            }
77
        }
78
79
        return false;
80
    }
81
}
82