1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
/**
21
 * A PHP code sniffer task. Checking the style of one or more PHP source files.
22
 *
23
 * @author  Siad Ardroumli <siad.ardroumli@gmail.com>
24
 * @package phing.tasks.ext
25
 */
26
class PhpCSTask extends Task
27
{
28
    use LogLevelAware;
29
    use FileSetAware;
30

31
    /**
32
     * A php source code filename or directory
33
     *
34
     * @var PhingFile
35
     */
36
    private $file;
37

38
    /**
39
     * The
40
     *
41
     * @var array
42
     */
43
    protected $files = [];
44

45

46
    /** @var Commandline */
47
    private $cmd;
48

49
    /** @var bool */
50
    private $cache = false;
51

52
    /** @var bool */
53
    private $ignoreAnnotations = false;
54

55
    /** @var bool */
56
    private $checkreturn = false;
57

58
    /** @var string */
59
    private $bin = 'phpcs';
60

61 1
    public function __construct()
62
    {
63 1
        $this->cmd = new Commandline();
64 1
        $this->logLevelName = 'info';
65 1
        parent::__construct();
66
    }
67

68 1
    public function getCommandline(): Commandline
69
    {
70 1
        return $this->cmd;
71
    }
72

73
    /**
74
     * @param bool $cache
75
     */
76 0
    public function setCache(bool $cache): void
77
    {
78 0
        $this->cache = $cache;
79
    }
80

81
    /**
82
     * @param bool $ignore
83
     */
84 1
    public function setIgnoreAnnotations(bool $ignore): void
85
    {
86 1
        $this->ignoreAnnotations = $ignore;
87
    }
88

89
    /**
90
     * @param bool $checkreturn
91
     */
92 1
    public function setCheckreturn(bool $checkreturn): void
93
    {
94 1
        $this->checkreturn = $checkreturn;
95
    }
96

97
    /**
98
     * @param string $bin
99
     */
100 1
    public function setBin(string $bin): void
101
    {
102 1
        $this->bin = $bin;
103
    }
104

105
    /**
106
     * @param PhingFile $file
107
     */
108 1
    public function setFile(PhingFile $file): void
109
    {
110 1
        $this->file = $file;
111
    }
112

113 1
    public function main()
114
    {
115 1
        if ($this->file === null && count($this->filesets) == 0) {
116 1
            throw new BuildException('Missing both attribute "file" and "fileset".');
117
        }
118 1
        if ($this->file === null) {
119
            // check filesets, and compile a list of files for phpcs to analyse
120 0
            foreach ($this->filesets as $fileset) {
121 0
                $files = $fileset->getIterator();
122 0
                foreach ($files as $file) {
123 0
                    $this->files[] = $file;
124
                }
125
            }
126
        }
127

128 1
        $toExecute = $this->getCommandline();
129

130 1
        $this->cache
131 0
            ? $toExecute->createArgument()->setValue('--cache')
132 1
            : $toExecute->createArgument()->setValue('--no-cache');
133

134 1
        if ($this->ignoreAnnotations) {
135 1
            $toExecute->createArgument()->setValue('--ignore-annotations');
136
        }
137

138 1
        if ($this->file !== null) {
139 1
            $toExecute->createArgument()->setFile($this->file);
140
        } else {
141 0
            foreach ($this->files as $file) {
142 0
                $toExecute->createArgument()->setFile(new PhingFile($file));
143
            }
144
        }
145

146 1
        $exe = new ExecTask();
147 1
        $exe->setProject($this->getProject());
148 1
        $exe->setLocation($this->getLocation());
149 1
        $exe->setOwningTarget($this->target);
150 1
        $exe->setTaskName($this->getTaskName());
151 1
        $exe->setExecutable($this->bin);
152 1
        $exe->setCheckreturn($this->checkreturn);
153 1
        $exe->setLevel($this->logLevelName);
154 1
        $exe->setExecutable($toExecute->getExecutable());
155 1
        $exe->createArg()->setLine(implode(' ', $toExecute->getArguments()));
156 1
        $exe->main();
157
    }
158
}

Read our documentation on viewing source code .

Loading