Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
LogsCommandResults
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 log
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 success
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 failure
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Fast Forward Development Tools for PHP projects.
7 *
8 * This file is part of fast-forward/dev-tools project.
9 *
10 * @author   Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
11 * @license  https://opensource.org/licenses/MIT MIT License
12 *
13 * @see      https://github.com/php-fast-forward/
14 * @see      https://github.com/php-fast-forward/dev-tools
15 * @see      https://github.com/php-fast-forward/dev-tools/issues
16 * @see      https://php-fast-forward.github.io/dev-tools/
17 * @see      https://datatracker.ietf.org/doc/html/rfc2119
18 */
19
20namespace FastForward\DevTools\Console\Command\Traits;
21
22use Psr\Log\LogLevel;
23use Symfony\Component\Console\Command\Command;
24use Symfony\Component\Console\Input\InputInterface;
25
26/**
27 * Provides reusable helpers for logging command outcomes and returning exit codes.
28 *
29 * Consuming commands stay focused on orchestration while this trait keeps the
30 * success, notice, and failure logging shape consistent across the command
31 * surface.
32 */
33trait LogsCommandResults
34{
35    use HasCommandLogger;
36
37    /**
38     * Logs a non-terminal command message unless structured JSON output is active.
39     *
40     * @param string $message the progress message
41     * @param InputInterface $input the originating command input
42     * @param array<string, mixed> $context optional extra log context
43     * @param string $logLevel the PSR-3 log level used for the message
44     *
45     * @return void
46     */
47    private function log(
48        string $message,
49        InputInterface $input,
50        array $context = [],
51        string $logLevel = LogLevel::INFO,
52    ): void {
53        if (method_exists($this, 'isJsonOutput') && $this->isJsonOutput($input)) {
54            return;
55        }
56
57        $context = [
58            'input' => $input,
59            ...$context,
60        ];
61
62        $this->getLogger()
63            ->log($logLevel, $message, $context);
64    }
65
66    /**
67     * Logs a successful command result and returns the success exit code.
68     *
69     * @param string $message the success message
70     * @param InputInterface $input the originating command input
71     * @param array<string, mixed> $context optional extra log context
72     * @param string $logLevel the PSR-3 log level used for the successful result
73     *
74     * @return int
75     */
76    private function success(
77        string $message,
78        InputInterface $input,
79        array $context = [],
80        string $logLevel = LogLevel::INFO,
81    ): int {
82        $context = [
83            'input' => $input,
84            ...$context,
85        ];
86
87        $this->getLogger()
88            ->log($logLevel, $message, $context);
89
90        return Command::SUCCESS;
91    }
92
93    /**
94     * Logs a failed command result and returns the failure exit code.
95     *
96     * @param string $message the failure message
97     * @param InputInterface $input the originating command input
98     * @param array<string, mixed> $context optional extra log context
99     * @param string|null $file the related file path when known
100     * @param int|null $line the related line when known
101     *
102     * @return int
103     */
104    private function failure(
105        string $message,
106        InputInterface $input,
107        array $context = [],
108        ?string $file = null,
109        ?int $line = null,
110    ): int {
111        $this->getLogger()
112            ->error($message, [
113                'input' => $input,
114                'file' => $file,
115                'line' => $line,
116                ...$context,
117            ]);
118
119        return Command::FAILURE;
120    }
121}