Issues (2)

src/Console/AlphabetGenerateCommand.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace ElfSundae\Laravel\Hashid\Console;
4
5
use Illuminate\Console\Command;
6
use Symfony\Component\Console\Input\InputOption;
7
8
class AlphabetGenerateCommand extends Command
9
{
10
    /**
11
     * The console command name.
12
     *
13
     * @var string
14
     */
15
    protected $name = 'hashid:alphabet';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Generate random alphabets';
23
24
    /**
25
     * The default characters.
26
     *
27
     * @var string
28
     */
29
    protected $defaultCharacters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
30
31
    /**
32
     * Execute the console command.
33
     *
34
     * @return mixed
35
     */
36 4
    public function handle()
37
    {
38 4
        $alphabets = $this->generateRandomAlphabets(
39 4
            $this->getTimes(),
40 4
            (string) $this->option('characters')
41 2
        );
42
43 4
        $this->comment(implode(PHP_EOL, $alphabets));
44 2
    }
45
46
    /**
47
     * Get "times" option value.
48
     *
49
     * @return int
50
     */
51 4
    protected function getTimes()
52
    {
53 4
        return max(1, min(100, (int) $this->option('times')));
54
    }
55
56
    /**
57
     * Generate random alphabets.
58
     *
59
     * @param  int  $times
60
     * @param  string  $characters
61
     * @return array
62
     */
63 4
    protected function generateRandomAlphabets($times = 1, $characters = null)
64
    {
65 4
        $characters = $characters ?: $this->defaultCharacters;
66
67 4
        $result = [];
68 4
        for ($i = 0; $i < $times; $i++) {
69 4
            $result[] = str_shuffle(count_chars($characters, 3));
0 ignored issues
show
It seems like count_chars($characters, 3) can also be of type array; however, parameter $string of str_shuffle() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
            $result[] = str_shuffle(/** @scrutinizer ignore-type */ count_chars($characters, 3));
Loading history...
70
        }
71
72 4
        return $result;
73
    }
74
75
    /**
76
     * Get the console command options.
77
     *
78
     * @return array
79
     */
80 8
    protected function getOptions()
81
    {
82 4
        return [
83 8
            ['characters', 'c', InputOption::VALUE_OPTIONAL, 'Use custom characters', $this->defaultCharacters],
84 8
            ['times', 't', InputOption::VALUE_OPTIONAL, 'Times to generate', 1],
85 4
        ];
86
    }
87
}
88