1<?php
2/*
3 * This file is part of PHPUnit.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11/**
12 * Prettifies class and method names for use in TestDox documentation.
13 */
14class PHPUnit_Util_TestDox_NamePrettifier
15{
16    /**
17     * @var string
18     */
19    protected $prefix = 'Test';
20
21    /**
22     * @var string
23     */
24    protected $suffix = 'Test';
25
26    /**
27     * @var array
28     */
29    protected $strings = [];
30
31    /**
32     * Prettifies the name of a test class.
33     *
34     * @param string $name
35     *
36     * @return string
37     */
38    public function prettifyTestClass($name)
39    {
40        $title = $name;
41
42        if ($this->suffix !== null &&
43            $this->suffix == substr($name, -1 * strlen($this->suffix))) {
44            $title = substr($title, 0, strripos($title, $this->suffix));
45        }
46
47        if ($this->prefix !== null &&
48            $this->prefix == substr($name, 0, strlen($this->prefix))) {
49            $title = substr($title, strlen($this->prefix));
50        }
51
52        if (substr($title, 0, 1) == '\\') {
53            $title = substr($title, 1);
54        }
55
56        return $title;
57    }
58
59    /**
60     * Prettifies the name of a test method.
61     *
62     * @param string $name
63     *
64     * @return string
65     */
66    public function prettifyTestMethod($name)
67    {
68        $buffer = '';
69
70        if (!is_string($name) || strlen($name) == 0) {
71            return $buffer;
72        }
73
74        $string = preg_replace('#\d+$#', '', $name, -1, $count);
75
76        if (in_array($string, $this->strings)) {
77            $name = $string;
78        } elseif ($count == 0) {
79            $this->strings[] = $string;
80        }
81
82        if (substr($name, 0, 4) == 'test') {
83            $name = substr($name, 4);
84        }
85
86        if (strlen($name) == 0) {
87            return $buffer;
88        }
89
90        $name[0] = strtoupper($name[0]);
91
92        if (strpos($name, '_') !== false) {
93            return trim(str_replace('_', ' ', $name));
94        }
95
96        $max        = strlen($name);
97        $wasNumeric = false;
98
99        for ($i = 0; $i < $max; $i++) {
100            if ($i > 0 &&
101                ord($name[$i]) >= 65 &&
102                ord($name[$i]) <= 90) {
103                $buffer .= ' ' . strtolower($name[$i]);
104            } else {
105                $isNumeric = is_numeric($name[$i]);
106
107                if (!$wasNumeric && $isNumeric) {
108                    $buffer    .= ' ';
109                    $wasNumeric = true;
110                }
111
112                if ($wasNumeric && !$isNumeric) {
113                    $wasNumeric = false;
114                }
115
116                $buffer .= $name[$i];
117            }
118        }
119
120        return $buffer;
121    }
122
123    /**
124     * Sets the prefix of test names.
125     *
126     * @param string $prefix
127     */
128    public function setPrefix($prefix)
129    {
130        $this->prefix = $prefix;
131    }
132
133    /**
134     * Sets the suffix of test names.
135     *
136     * @param string $suffix
137     */
138    public function setSuffix($suffix)
139    {
140        $this->suffix = $suffix;
141    }
142}
143