Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make OCP\IL10N strict #8476

Merged
merged 4 commits into from
Feb 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/templates/installation.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<p><?php p($l->t('Your data directory and files are probably accessible from the internet because the .htaccess file does not work.'));?><br>
<?php print_unescaped($l->t(
'For information how to properly configure your server, please see the <a href="%s" target="_blank" rel="noreferrer noopener">documentation</a>.',
link_to_docs('admin-install')
[link_to_docs('admin-install')]
)); ?></p>
</fieldset>
<?php endif; ?>
Expand Down
10 changes: 5 additions & 5 deletions lib/private/App/DependencyAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,19 @@ private function analyzePhpVersion(array $dependencies) {
if (isset($dependencies['php']['@attributes']['min-version'])) {
$minVersion = $dependencies['php']['@attributes']['min-version'];
if ($this->compareSmaller($this->platform->getPhpVersion(), $minVersion)) {
$missing[] = (string)$this->l->t('PHP %s or higher is required.', $minVersion);
$missing[] = (string)$this->l->t('PHP %s or higher is required.', [$minVersion]);
}
}
if (isset($dependencies['php']['@attributes']['max-version'])) {
$maxVersion = $dependencies['php']['@attributes']['max-version'];
if ($this->compareBigger($this->platform->getPhpVersion(), $maxVersion)) {
$missing[] = (string)$this->l->t('PHP with a version lower than %s is required.', $maxVersion);
$missing[] = (string)$this->l->t('PHP with a version lower than %s is required.', [$maxVersion]);
}
}
if (isset($dependencies['php']['@attributes']['min-int-size'])) {
$intSize = $dependencies['php']['@attributes']['min-int-size'];
if ($intSize > $this->platform->getIntSize()*8) {
$missing[] = (string)$this->l->t('%sbit or higher PHP required.', $intSize);
$missing[] = (string)$this->l->t('%sbit or higher PHP required.', [$intSize]);
}
}
return $missing;
Expand Down Expand Up @@ -209,7 +209,7 @@ private function analyzeCommands(array $dependencies) {
}
$commandName = $this->getValue($command);
if (!$this->platform->isCommandKnown($commandName)) {
$missing[] = (string)$this->l->t('The command line tool %s could not be found', $commandName);
$missing[] = (string)$this->l->t('The command line tool %s could not be found', [$commandName]);
}
}
return $missing;
Expand All @@ -236,7 +236,7 @@ private function analyzeLibraries(array $dependencies) {
$libName = $this->getValue($lib);
$libVersion = $this->platform->getLibraryVersion($libName);
if (is_null($libVersion)) {
$missing[] = (string)$this->l->t('The library %s is not available.', $libName);
$missing[] = (string)$this->l->t('The library %s is not available.', [$libName]);
continue;
}

Expand Down
41 changes: 21 additions & 20 deletions lib/private/L10N/L10N.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
Expand Down Expand Up @@ -60,7 +61,6 @@ public function __construct(IFactory $factory, $app, $lang, array $files) {
$this->app = $app;
$this->lang = $lang;

$this->translations = [];
foreach ($files as $languageFile) {
$this->load($languageFile);
}
Expand All @@ -71,7 +71,7 @@ public function __construct(IFactory $factory, $app, $lang, array $files) {
*
* @return string language
*/
public function getLanguageCode() {
public function getLanguageCode(): string {
return $this->lang;
}

Expand All @@ -84,7 +84,7 @@ public function getLanguageCode() {
* Returns the translation. If no translation is found, $text will be
* returned.
*/
public function t($text, $parameters = array()) {
public function t(string $text, array $parameters = []): string {
return (string) new L10NString($this, $text, $parameters);
}

Expand All @@ -103,17 +103,17 @@ public function t($text, $parameters = array()) {
* provided by the po file.
*
*/
public function n($text_singular, $text_plural, $count, $parameters = array()) {
public function n(string $text_singular, string $text_plural, int $count, array $parameters = []): string {
$identifier = "_${text_singular}_::_${text_plural}_";
if (isset($this->translations[$identifier])) {
return (string) new L10NString($this, $identifier, $parameters, $count);
} else {
if ($count === 1) {
return (string) new L10NString($this, $text_singular, $parameters, $count);
} else {
return (string) new L10NString($this, $text_plural, $parameters, $count);
}
}

if ($count === 1) {
return (string) new L10NString($this, $text_singular, $parameters, $count);
}

return (string) new L10NString($this, $text_plural, $parameters, $count);
}

/**
Expand All @@ -138,7 +138,7 @@ public function n($text_singular, $text_plural, $count, $parameters = array()) {
* - firstday: Returns the first day of the week (0 sunday - 6 saturday)
* - jsdate: Returns the short JS date format
*/
public function l($type, $data = null, $options = array()) {
public function l(string $type, $data = null, array $options = []) {
// Use the language of the instance
$locale = $this->getLanguageCode();
if ($locale === 'sr@latin') {
Expand All @@ -155,14 +155,15 @@ public function l($type, $data = null, $options = array()) {
$value = new \DateTime();
if ($data instanceof \DateTime) {
$value = $data;
} else if (is_string($data) && !is_numeric($data)) {
} else if (\is_string($data) && !is_numeric($data)) {
$data = strtotime($data);
$value->setTimestamp($data);
} else if ($data !== null) {
$data = (int)$data;
$value->setTimestamp($data);
}

$options = array_merge(array('width' => 'long'), $options);
$options = array_merge(['width' => 'long'], $options);
$width = $options['width'];
switch ($type) {
case 'date':
Expand All @@ -184,18 +185,18 @@ public function l($type, $data = null, $options = array()) {
* Called by \OC_L10N_String
* @return array
*/
public function getTranslations() {
public function getTranslations(): array {
return $this->translations;
}

/**
* Returnsed function accepts the argument $n
*
* Called by \OC_L10N_String
* @return string the plural form function
* @return \Closure the plural form function
*/
public function getPluralFormFunction() {
if (is_null($this->pluralFormFunction)) {
public function getPluralFormFunction(): \Closure {
if (\is_null($this->pluralFormFunction)) {
$lang = $this->getLanguageCode();
$this->pluralFormFunction = function($n) use ($lang) {
return PluralizationRules::get($n, $lang);
Expand All @@ -206,12 +207,12 @@ public function getPluralFormFunction() {
}

/**
* @param $translationFile
* @param string $translationFile
* @return bool
*/
protected function load($translationFile) {
protected function load(string $translationFile): bool {
$json = json_decode(file_get_contents($translationFile), true);
if (!is_array($json)) {
if (!\is_array($json)) {
$jsonError = json_last_error();
\OC::$server->getLogger()->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']);
return false;
Expand Down
9 changes: 5 additions & 4 deletions lib/public/IL10N.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
Expand Down Expand Up @@ -51,7 +52,7 @@ interface IL10N {
* returned.
* @since 6.0.0
*/
public function t($text, $parameters = array());
public function t(string $text, array $parameters = []): string;

/**
* Translating
Expand All @@ -69,7 +70,7 @@ public function t($text, $parameters = array());
* @since 6.0.0
*
*/
public function n($text_singular, $text_plural, $count, $parameters = array());
public function n(string $text_singular, string $text_plural, int $count, array $parameters = []): string;

/**
* Localization
Expand All @@ -96,7 +97,7 @@ public function n($text_singular, $text_plural, $count, $parameters = array());
* - params: timestamp (int/string)
* @since 6.0.0 - parameter $options was added in 8.0.0
*/
public function l($type, $data, $options = array());
public function l(string $type, $data, array $options = []);


/**
Expand All @@ -105,5 +106,5 @@ public function l($type, $data, $options = array());
* @return string language
* @since 7.0.0
*/
public function getLanguageCode();
public function getLanguageCode(): string ;
}