-
Notifications
You must be signed in to change notification settings - Fork 1
/
Extension.php
50 lines (39 loc) · 1.52 KB
/
Extension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
declare(strict_types=1);
namespace Babelfish\Strategy;
use Babelfish\File\SourceFile;
use Babelfish\Language;
use Babelfish\Strategy\Filter\OnlyKeepLanguageAlreadyCandidatesFilter;
use function pathinfo;
use const PATHINFO_EXTENSION;
final class Extension implements Strategy
{
/** @var OnlyKeepLanguageAlreadyCandidatesFilter */
private $filter;
public function __construct(OnlyKeepLanguageAlreadyCandidatesFilter $filter)
{
$this->filter = $filter;
}
/**
* @return Language[]
*/
public function getLanguages(SourceFile $file, Language ...$language_candidates): array
{
/** @psalm-var array{extension: string, filename: string} $path_information */
$path_information = pathinfo($file->getName());
if (! isset($path_information['extension'])) {
return [];
}
$extension = $path_information['extension'];
$second_extension_part = pathinfo($path_information['filename'], PATHINFO_EXTENSION);
if ($second_extension_part !== '') {
$composite_extension = '.' . $second_extension_part . '.' . $extension;
$languages = Language::findLanguagesByExtension($composite_extension);
if (! empty($languages)) {
return $this->filter->filter($language_candidates, ...$languages);
}
}
$languages = Language::findLanguagesByExtension('.' . $extension);
return $this->filter->filter($language_candidates, ...$languages);
}
}