• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JBZoo / Cli / 18073296850

28 Sep 2025 10:57AM UTC coverage: 80.982%. Remained the same
18073296850

Pull #32

github

web-flow
Merge e115b5888 into cb3513d8b
Pull Request #32: refactor(cli): Refine code and static analysis

1 of 1 new or added line in 1 file covered. (100.0%)

2 existing lines in 1 file now uncovered.

1056 of 1304 relevant lines covered (80.98%)

229.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.67
/src/CliApplication.php
1
<?php
2

3
/**
4
 * JBZoo Toolbox - Cli.
5
 *
6
 * This file is part of the JBZoo Toolbox project.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT
11
 * @copyright  Copyright (C) JBZoo.com, All rights reserved.
12
 * @see        https://github.com/JBZoo/Cli
13
 */
14

15
declare(strict_types=1);
16

17
namespace JBZoo\Cli;
18

19
use JBZoo\Cli\OutputMods\AbstractOutputMode;
20
use JBZoo\Cli\OutputMods\Text;
21
use JBZoo\Event\EventManager;
22
use JBZoo\Utils\FS;
23
use Symfony\Component\Console\Application;
24
use Symfony\Component\Console\Output\OutputInterface;
25

26
use function JBZoo\Utils\isStrEmpty;
27

28
final class CliApplication extends Application
29
{
30
    private ?EventManager       $eventManager = null;
31
    private ?string             $logo         = null;
32
    private ?AbstractOutputMode $outputMode   = null;
33

34
    /**
35
     * Register commands by directory path.
36
     */
37
    public function registerCommandsByPath(
38
        string $commandsDir,
39
        string $gloabalNamespace,
40
        bool $strictMode = true,
41
    ): self {
42
        if ($strictMode && !\is_dir($commandsDir)) {
846✔
43
            throw new Exception('First argument is not directory!');
×
44
        }
45

46
        /** @var string[] $files */
47
        $files = FS::ls($commandsDir);
846✔
48
        $files = \array_filter($files, static function (string $file) {
846✔
49
            return \str_ends_with($file, '.php');
846✔
50
        });
846✔
51

52
        if (\count($files) === 0) {
846✔
53
            return $this;
×
54
        }
55

56
        foreach ($files as $file) {
846✔
57
            if (!\file_exists($file)) {
846✔
58
                continue;
×
59
            }
60

61
            require_once $file;
846✔
62

63
            $taskNamespace    = \trim(\str_replace('/', '\\', (string)\strstr(\dirname($file), 'Commands')));
846✔
64
            $commandClassName = "{$gloabalNamespace}\\{$taskNamespace}\\" . FS::filename($file);
846✔
65

66
            if (\class_exists($commandClassName)) {
846✔
67
                $reflection = new \ReflectionClass($commandClassName);
846✔
68
            } else {
69
                throw new Exception("Command/Class \"{$commandClassName}\" can'be loaded from the file \"{$file}\"");
×
70
            }
71

72
            if (!$reflection->isAbstract() && $reflection->isSubclassOf(CliCommand::class)) {
846✔
73
                $command = $reflection->newInstance();
846✔
74
                $this->add($command);
846✔
75
            }
76
        }
77

78
        return $this;
846✔
79
    }
80

81
    public function setEventManager(EventManager $eventManager): self
82
    {
83
        $this->eventManager = $eventManager;
×
84

85
        return $this;
×
86
    }
87

88
    public function getEventManager(): ?EventManager
89
    {
90
        return $this->eventManager;
846✔
91
    }
92

93
    public function setLogo(?string $logo = null): self
94
    {
95
        $this->logo = $logo;
×
96

97
        return $this;
×
98
    }
99

100
    public function renderThrowable(\Throwable $exception, OutputInterface $output): void
101
    {
102
        if ($this->outputMode === null || $this->outputMode instanceof Text) {
132✔
103
            parent::renderThrowable($exception, $output);
90✔
104
        }
105
    }
106

107
    /**
108
     * Returns the long version of the application.
109
     */
110
    public function getLongVersion(): string
111
    {
UNCOV
112
        if (!isStrEmpty($this->logo)) {
×
UNCOV
113
            return "<info>{$this->logo}</info>\n<comment>{$this->getVersion()}</comment>";
×
114
        }
115

116
        return parent::getLongVersion();
×
117
    }
118

119
    public function setOutputMode(AbstractOutputMode $outputMode): self
120
    {
121
        $this->outputMode = $outputMode;
846✔
122

123
        return $this;
846✔
124
    }
125
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc