Completed
Push — master ( 130d29...c9c04e )
by Tobias
187:22 queued 181:29
created

CatalogueWriter::writeCatalogues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 14
cp 0
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Bundle\Catalogue;
13
14
use Symfony\Component\Translation\MessageCatalogue;
15
use Symfony\Component\Translation\Writer\TranslationWriter;
16
use Symfony\Component\Translation\Writer\TranslationWriterInterface;
17
use Translation\Bundle\Model\Configuration;
18
use Translation\SymfonyStorage\LegacyTranslationWriter;
19
20
/**
21
 * Write catalogues back to disk.
22
 *
23
 * This should be considered as a "WriteToCache" service.
24
 *
25
 * @author Tobias Nyholm <[email protected]>
26
 */
27
final class CatalogueWriter
28
{
29
    /**
30
     * @var TranslationWriterInterface
31
     */
32
    private $writer;
33
34
    /**
35
     * @var string
36
     */
37
    private $defaultLocale;
38
39
    /**
40
     * @param TranslationWriter $writer
41
     * @param string            $defaultLocale
42
     */
43
    public function __construct(TranslationWriter $writer, $defaultLocale)
44
    {
45
        if (!$writer instanceof TranslationWriterInterface) {
46
            $writer = new LegacyTranslationWriter($writer);
47
        }
48
49
        $this->writer = $writer;
0 ignored issues
show
Documentation Bug introduced by
It seems like $writer can also be of type object<Translation\Symfo...egacyTranslationWriter>. However, the property $writer is declared as type object<Symfony\Component...slationWriterInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
50
        $this->defaultLocale = $defaultLocale;
51
    }
52
53
    /**
54
     * @param Configuration      $config
55
     * @param MessageCatalogue[] $catalogues
56
     */
57
    public function writeCatalogues(Configuration $config, array $catalogues)
58
    {
59
        foreach ($catalogues as $catalogue) {
60
            $this->writer->write(
61
                $catalogue,
62
                $config->getOutputFormat(),
63
                [
64
                    'path' => $config->getOutputDir(),
65
                    'default_locale' => $this->defaultLocale,
66
                    'xliff_version' => $config->getXliffVersion(),
67
                ]
68
            );
69
        }
70
    }
71
}
72