Skip to content

Commit

Permalink
Adjusted to PHPStan level 9
Browse files Browse the repository at this point in the history
  • Loading branch information
intelektron committed Jan 14, 2024
1 parent 4ba3e51 commit 0e9ac48
Show file tree
Hide file tree
Showing 18 changed files with 167 additions and 95 deletions.
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
"phpstan/phpstan-strict-rules": "^1.5",
"thecodingmachine/phpstan-strict-rules": "^1.0",
"phpstan/extension-installer": "^1.3",
"voku/phpstan-rules": "^3.2",
"shipmonk/dead-code-detector": "^0.1.0",
"sidz/phpstan-rules": "^0.4.3"
"voku/phpstan-rules": "^3.2"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 5
level: 9
paths:
- src

Expand Down
3 changes: 3 additions & 0 deletions src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

class Block
{
/**
* @param Chord[] $chords The chords.
*/
public function __construct(private array $chords, private string $text)
{
}
Expand Down
20 changes: 15 additions & 5 deletions src/Chord.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ class Chord

/**
* Static cache of different notation roots.
*
* @var string[][]
*/
private static $notationRoots = [];
private static array $notationRoots = [];

public function __construct(private string $originalName, ?ChordNotationInterface $sourceNotation = null)
{
Expand All @@ -50,9 +52,14 @@ public function __construct(private string $originalName, ?ChordNotationInterfac
}
}

/**
* Create multiple chords from slices like [C/E].
*
* @return Chord[]
*/
public static function fromSlice(string $text, ?ChordNotationInterface $notation = null): array
{
if (empty($text)) {
if ($text === '') {
return [];
}
$chords = explode('/', $text);
Expand All @@ -73,9 +80,12 @@ public function isMinor(): bool
return substr($this->rootChord, -1) === 'm';
}

/**
* @return string[] The root chords in the notation.
*/
private function getRootChordTable(?ChordNotationInterface $notation): array
{
if (!$notation) {
if (is_null($notation)) {
return self::ROOT_CHORDS;
} elseif (isset($this->notationRoots[$notation::class])) {
return $this->notationRoots[$notation::class];
Expand All @@ -91,15 +101,15 @@ private function getRootChordTable(?ChordNotationInterface $notation): array

public function getRootChord(?ChordNotationInterface $targetNotation = null): string
{
if ($targetNotation) {
if (!is_null($targetNotation)) {
return $targetNotation->convertChordRootToNotation($this->rootChord);
}
return $this->rootChord;
}

public function getExt(?ChordNotationInterface $targetNotation = null): string
{
if ($targetNotation) {
if (!is_null($targetNotation)) {
return $targetNotation->convertExtToNotation($this->ext);
}
return $this->ext;
Expand Down
9 changes: 8 additions & 1 deletion src/Formatter/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ abstract class Formatter
{
protected ?ChordNotationInterface $notation;
protected bool $noChords = false;

/**
* @var string[]
*/
protected array $ignoreMetadata = [];

/**
* @param mixed[] $options
*/
public function setOptions(array $options): void
{
if (isset($options['notation']) && $options['notation'] instanceof ChordNotationInterface) {
Expand All @@ -20,7 +27,7 @@ public function setOptions(array $options): void
if (isset($options['no_chords']) && true === $options['no_chords']) {
$this->noChords = true;
}
if (!empty($options['ignore_metadata']) && is_array($options['ignore_metadata'])) {
if (isset($options['ignore_metadata']) && is_array($options['ignore_metadata'])) {
$this->ignoreMetadata = $options['ignore_metadata'];
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/Formatter/FormatterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@

interface FormatterInterface
{
/**
* @param mixed[] $options
*/
public function format(Song $song, array $options): string;
}
24 changes: 11 additions & 13 deletions src/Formatter/HtmlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private function getLineHtml(Line $line): string
private function blankChars(?string $text): string
{
// @todo Is this if needed?
if (empty($text)) {
if (is_null($text) || $text === '') {
$text = ' ';
}
return str_replace(' ', ' ', $text);
Expand All @@ -48,41 +48,39 @@ private function blankChars(?string $text): string
private function getMetadataHtml(Metadata $metadata): string
{
// Ignore some metadata.
if (in_array($metadata->getName(), $this->ignoreMetadata)) {
if (in_array($metadata->getName(), $this->ignoreMetadata, true)) {
return '';
}

$match = [];
if (preg_match('/^start_of_(.*)/', $metadata->getName(), $match)) {
if (preg_match('/^start_of_(.*)/', $metadata->getName(), $match) !== false) {
$type = preg_replace('/[\W_\-]/', '', $match[1]);
$content = '';
if (null !== $metadata->getValue()) {
$content = '<div class="chordpro-'.$type.'-comment">'.$metadata->getValue().'</div>';
}
return $content.'<div class="chordpro-'.$type.'">';
} elseif (preg_match('/^end_of_(.*)/', $metadata->getName())) {
} elseif (preg_match('/^end_of_(.*)/', $metadata->getName()) !== false) {
return '</div>';
} else {
$name = preg_replace('/[\W_\-]/', '', mb_strtolower($metadata->getName()));
return '<div class="chordpro-'.$name.'">'.$metadata->getValue().'</div>';
}
}

private function getLyricsHtml(Lyrics $lyrics)
private function getLyricsHtml(Lyrics $lyrics): string
{
$verse = '<div class="chordpro-verse">';
foreach ($lyrics->getBlocks() as $block) {

$chords = [];

$slicedChords = $block->getChords();
if (!empty($slicedChords)) {
foreach ($slicedChords as $slicedChord) {
if ($slicedChord->isKnown()) {
$chords[] = $slicedChord->getRootChord($this->notation).'<sup>'.$slicedChord->getExt($this->notation).'</sup>';
} else {
$chords[] = $slicedChord->getOriginalName();
}
foreach ($slicedChords as $slicedChord) {
if ($slicedChord->isKnown()) {
$chords[] = $slicedChord->getRootChord($this->notation).'<sup>'.$slicedChord->getExt($this->notation).'</sup>';
} else {
$chords[] = $slicedChord->getOriginalName();
}
}

Expand All @@ -98,7 +96,7 @@ private function getLyricsHtml(Lyrics $lyrics)
return $verse;
}

private function getLyricsOnlyHtml(Lyrics $lyrics)
private function getLyricsOnlyHtml(Lyrics $lyrics): string
{
$verse = '<div class="chordpro-verse">';
foreach ($lyrics->getBlocks() as $block) {
Expand Down
30 changes: 18 additions & 12 deletions src/Formatter/JSONFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ public function format(Song $song, array $options): string
foreach ($song->getLines() as $line) {
$json[] = $this->getLineJSON($line);
}
return json_encode($json, JSON_PRETTY_PRINT);
return (string) json_encode($json, JSON_PRETTY_PRINT);
}


private function getLineJSON(Line $line)
/**
* @return mixed
*/
private function getLineJSON(Line $line): mixed
{
if ($line instanceof Metadata) {
return $this->getMetadataJSON($line);
Expand All @@ -34,14 +36,17 @@ private function getLineJSON(Line $line)
}
}

/**
* @return mixed[]
*/
private function getMetadataJSON(Metadata $metadata): ?array
{
// Ignore some metadata.
if (in_array($metadata->getName(), $this->ignoreMetadata)) {
if (in_array($metadata->getName(), $this->ignoreMetadata, true)) {
return null;
}

if (empty($metadata->getValue())) {
if (!is_null($metadata->getValue())) {
return [$metadata->getName()];
} else {
switch($metadata->getName()) {
Expand All @@ -51,19 +56,20 @@ private function getMetadataJSON(Metadata $metadata): ?array
}
}

/**
* @return mixed[]
*/
private function getLyricsJSON(Lyrics $lyrics): array
{
$return = [];
foreach ($lyrics->getBlocks() as $block) {
$chords = [];
$slicedChords = $block->getChords();
if (!empty($slicedChords)) {
foreach ($slicedChords as $slicedChord) {
if ($slicedChord->isKnown()) {
$chords[] = $slicedChord->getRootChord($this->notation).$slicedChord->getExt($this->notation);
} else {
$chords[] = $slicedChord->getOriginalName();
}
foreach ($slicedChords as $slicedChord) {
if ($slicedChord->isKnown()) {
$chords[] = $slicedChord->getRootChord($this->notation).$slicedChord->getExt($this->notation);
} else {
$chords[] = $slicedChord->getOriginalName();
}
}
$chord = implode('/', array_map("implode", $chords)).' ';
Expand Down
24 changes: 11 additions & 13 deletions src/Formatter/MonospaceFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,22 @@ private function getLineMonospace(Line $line): string
private function getMetadataMonospace(Metadata $metadata): string
{
// Ignore some metadata.
if (in_array($metadata->getName(), $this->ignoreMetadata)) {
if (in_array($metadata->getName(), $this->ignoreMetadata, true)) {
return '';
}

$match = [];
if (preg_match('/^start_of_(.*)/', $metadata->getName(), $match)) {
if (preg_match('/^start_of_(.*)/', $metadata->getName(), $match) !== false) {
$content = (null !== $metadata->getValue()) ? $metadata->getValue()."\n" : mb_strtoupper($match[1]) . "\n";
return $content;
} elseif (preg_match('/^end_of_(.*)/', $metadata->getName())) {
} elseif (preg_match('/^end_of_(.*)/', $metadata->getName()) !== false) {
return '\n';
} else {
return $metadata->getValue()."\n";
}
}

private function generateBlank($count)
private function generateBlank(int $count): string
{
$i = 1;
$blank = '';
Expand All @@ -67,21 +67,19 @@ private function generateBlank($count)
return $blank;
}

private function getLyricsMonospace(Lyrics $lyrics)
private function getLyricsMonospace(Lyrics $lyrics): string
{
$lineChords = '';
$lineTexts = '';

foreach ($lyrics->getBlocks() as $block) {
$chords = [];
$slicedChords = $block->getChords();
if (!empty($slicedChords)) {
foreach ($slicedChords as $slicedChord) {
if ($slicedChord->isKnown()) {
$chords[] = $slicedChord->getRootChord($this->notation).$slicedChord->getExt($this->notation);
} else {
$chords[] = $slicedChord->getOriginalName();
}
foreach ($slicedChords as $slicedChord) {
if ($slicedChord->isKnown()) {
$chords[] = $slicedChord->getRootChord($this->notation).$slicedChord->getExt($this->notation);
} else {
$chords[] = $slicedChord->getOriginalName();
}
}

Expand All @@ -99,7 +97,7 @@ private function getLyricsMonospace(Lyrics $lyrics)
return $lineChords."\n".$lineTexts."\n";
}

private function getLyricsOnlyMonospace(Lyrics $lyrics)
private function getLyricsOnlyMonospace(Lyrics $lyrics): string
{
$texts = '';
foreach ($lyrics->getBlocks() as $block) {
Expand Down
28 changes: 19 additions & 9 deletions src/GuessKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
*/
class GuessKey
{
private $scales = [
/**
* @var string[][]
*/
private array $scales = [
'A' => ['A','Bm','C#m','D','E','F#m','G#'],
'A#' => ['A#','B#m','Dm','D#','E#','Gm','A'],
'Bb' => ['Bb','Cm','Dm','Eb','F','Gm','A'],
Expand All @@ -39,7 +42,10 @@ class GuessKey
'Ab' => ['Ab','Bbm','Cm','Db','Eb','Fm','G']
];

private $distanceChords = [
/**
* @var int[]
*/
private array $distanceChords = [
'C' => 0,
'C#' => 1,
'Db' => 1,
Expand All @@ -59,22 +65,25 @@ class GuessKey
'B' => 11,
];

private function nearChords($chord) // I don't know how to call this exactly in english ? « Tons voisin » in french.
/**
* @return string[]
*/
private function nearChords(string $chord): array // I don't know how to call this exactly in english ? « Tons voisin » in french.
{
$distance = ($this->distanceChords[$chord] - 3 < 0) ? ($this->distanceChords[$chord] - 3) + 12 : $this->distanceChords[$chord] - 3;
$found = array_keys($this->distanceChords, $distance);
$found = array_keys($this->distanceChords, $distance, true);
return $found;
}

public function guessKey($song)
public function guessKey(Song $song): string
{
// List all chords of a song
$chords = [];
foreach ($song->getLines() as $line) {
if ($line instanceof Lyrics) {
foreach ($line->getBlocks() as $block) {
$blockChords = $block->getChords();
if (!empty($blockChords)) {
if (count($blockChords) > 0) {
$chords[] = reset($blockChords);
}
}
Expand All @@ -86,7 +95,7 @@ public function guessKey($song)
foreach ($this->scales as $key => $scale) {
$listKeys[$key] = 0;
foreach ($chords as $chord) {
if (in_array($chord->getRootChord(), $scale)) {
if (in_array($chord->getRootChord(), $scale, true)) {
$listKeys[$key]++;
}
}
Expand All @@ -97,10 +106,11 @@ public function guessKey($song)
$minorKeys = $this->nearChords($majorKey); // Find minors keys near major key

// Count occurences of minor & major keys to determinate the most plausible key
$result[$majorKey] = count(array_keys($chords, $majorKey));
$result = [];
$result[$majorKey] = count(array_keys($chords, $majorKey, true));
foreach ($minorKeys as $key) {
$key = $key.'m';
$count = count(array_keys($chords, $key));
$count = count(array_keys($chords, $key, true));
if ($count > 0) {
$result[$key] = $count;
}
Expand Down
Loading

0 comments on commit 0e9ac48

Please sign in to comment.