Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
Author
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
6 / 6
12
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEmail
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHomepage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRole
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __toString
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
7
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\Composer\Json\Schema;
21
22/**
23 * Concrete implementation of the ComposerJsonAuthorInterface.
24 *
25 * This class represents an author entry within a composer.json file and provides
26 * structured access to author metadata such as name, email, homepage, and role.
27 *
28 * Implementations of this class MUST ensure that all provided data is consistent
29 * and valid according to Composer expectations. Consumers of this class MAY rely
30 * on its immutability if used in a readonly context.
31 *
32 * The string representation of this class SHALL return a human-readable
33 * representation of the author.
34 */
35  class Author implements AuthorInterface
36{
37    /**
38     * Constructs a new ComposerJsonAuthor instance.
39     *
40     * All parameters MUST be provided as strings. Implementations SHOULD validate
41     * the correctness of email and URL formats before assignment if strict validation is required.
42     *
43     * @param string $name the name of the author
44     * @param string $email the email address of the author
45     * @param string $homepage the homepage URL of the author
46     * @param string $role the role of the author
47     */
48    public function __construct(
49        private string $name = '',
50        private string $email = '',
51        private string $homepage = '',
52        private string $role = ''
53    ) {}
54
55    /**
56     * {@inheritDoc}
57     */
58    public function getName(): string
59    {
60        return $this->name;
61    }
62
63    /**
64     * {@inheritDoc}
65     */
66    public function getEmail(): string
67    {
68        return $this->email;
69    }
70
71    /**
72     * {@inheritDoc}
73     */
74    public function getHomepage(): string
75    {
76        return $this->homepage;
77    }
78
79    /**
80     * {@inheritDoc}
81     */
82    public function getRole(): string
83    {
84        return $this->role;
85    }
86
87    /**
88     * Returns a string representation of the author.
89     *
90     * This method SHALL return a formatted string combining the author's name and email.
91     * Implementations MAY extend this format but SHOULD maintain readability.
92     *
93     * @return string the string representation of the author
94     */
95    public function __toString(): string
96    {
97        if (! $this->name && ! $this->email) {
98            return '';
99        }
100
101        if ($this->name && ! $this->email) {
102            return $this->name;
103        }
104
105        if (! $this->name && $this->email) {
106            return $this->email;
107        }
108
109        return \sprintf('%s <%s>', $this->name, $this->email);
110    }
111}