Skip to content

Commit 5dbf840

Browse files
committed
Ability to add flags to scanned functions
1 parent b8ee221 commit 5dbf840

5 files changed

Lines changed: 50 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
Previous releases are documented in [github releases](https://github.com/oscarotero/Gettext/releases)
99

10+
## [5.6.0] - Unreleased
11+
### Added
12+
- New method `addFlag` to `ParsedFunction`, that allows to assign flags by scanners.
13+
- The `FunctionsHandlersTrait` has an abstract `addFlags` method.
14+
1015
## [5.5.4] - 2020-12-20
1116
### Fixed
1217
- TypeError in which numeric entries were converted to integers [#265]
@@ -97,6 +102,7 @@ Previous releases are documented in [github releases](https://github.com/oscarot
97102
[#263]: https://github.com/php-gettext/Gettext/issues/263
98103
[#265]: https://github.com/php-gettext/Gettext/issues/265
99104

105+
[5.6.0]: https://github.com/php-gettext/Gettext/compare/v5.5.4...HEAD
100106
[5.5.4]: https://github.com/php-gettext/Gettext/compare/v5.5.3...v5.5.4
101107
[5.5.3]: https://github.com/php-gettext/Gettext/compare/v5.5.2...v5.5.3
102108
[5.5.2]: https://github.com/php-gettext/Gettext/compare/v5.5.1...v5.5.2

src/Scanner/CodeScanner.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ protected function addComments(ParsedFunction $function, ?Translation $translati
109109
return $translation;
110110
}
111111

112+
protected function addFlags(ParsedFunction $function, ?Translation $translation): ?Translation
113+
{
114+
foreach ($function->getFlags() as $flag) {
115+
$translation->getFlags()->add($flag);
116+
}
117+
118+
return $translation;
119+
}
120+
112121
protected function checkFunction(ParsedFunction $function, int $minLength): bool
113122
{
114123
if ($function->countArguments() < $minLength) {

src/Scanner/FunctionsHandlersTrait.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ protected function gettext(ParsedFunction $function): ?Translation
1717
}
1818
list($original) = $function->getArguments();
1919

20-
return $this->addComments(
20+
$translation = $this->addComments(
2121
$function,
2222
$this->saveTranslation(null, null, $original)
2323
);
24+
return $this->addFlags($function, $translation);
2425
}
2526

2627
protected function ngettext(ParsedFunction $function): ?Translation
@@ -30,10 +31,11 @@ protected function ngettext(ParsedFunction $function): ?Translation
3031
}
3132
list($original, $plural) = $function->getArguments();
3233

33-
return $this->addComments(
34+
$translation = $this->addComments(
3435
$function,
3536
$this->saveTranslation(null, null, $original, $plural)
3637
);
38+
return $this->addFlags($function, $translation);
3739
}
3840

3941
protected function pgettext(ParsedFunction $function): ?Translation
@@ -43,10 +45,11 @@ protected function pgettext(ParsedFunction $function): ?Translation
4345
}
4446
list($context, $original) = $function->getArguments();
4547

46-
return $this->addComments(
48+
$translation = $this->addComments(
4749
$function,
4850
$this->saveTranslation(null, $context, $original)
4951
);
52+
return $this->addFlags($function, $translation);
5053
}
5154

5255
protected function dgettext(ParsedFunction $function): ?Translation
@@ -56,10 +59,11 @@ protected function dgettext(ParsedFunction $function): ?Translation
5659
}
5760
list($domain, $original) = $function->getArguments();
5861

59-
return $this->addComments(
62+
$translation = $this->addComments(
6063
$function,
6164
$this->saveTranslation($domain, null, $original)
6265
);
66+
return $this->addFlags($function, $translation);
6367
}
6468

6569
protected function dpgettext(ParsedFunction $function): ?Translation
@@ -69,10 +73,11 @@ protected function dpgettext(ParsedFunction $function): ?Translation
6973
}
7074
list($domain, $context, $original) = $function->getArguments();
7175

72-
return $this->addComments(
76+
$translation = $this->addComments(
7377
$function,
7478
$this->saveTranslation($domain, $context, $original)
7579
);
80+
return $this->addFlags($function, $translation);
7681
}
7782

7883
protected function npgettext(ParsedFunction $function): ?Translation
@@ -82,10 +87,11 @@ protected function npgettext(ParsedFunction $function): ?Translation
8287
}
8388
list($context, $original, $plural) = $function->getArguments();
8489

85-
return $this->addComments(
90+
$translation = $this->addComments(
8691
$function,
8792
$this->saveTranslation(null, $context, $original, $plural)
8893
);
94+
return $this->addFlags($function, $translation);
8995
}
9096

9197
protected function dngettext(ParsedFunction $function): ?Translation
@@ -95,10 +101,11 @@ protected function dngettext(ParsedFunction $function): ?Translation
95101
}
96102
list($domain, $original, $plural) = $function->getArguments();
97103

98-
return $this->addComments(
104+
$translation = $this->addComments(
99105
$function,
100106
$this->saveTranslation($domain, null, $original, $plural)
101107
);
108+
return $this->addFlags($function, $translation);
102109
}
103110

104111
protected function dnpgettext(ParsedFunction $function): ?Translation
@@ -108,14 +115,17 @@ protected function dnpgettext(ParsedFunction $function): ?Translation
108115
}
109116
list($domain, $context, $original, $plural) = $function->getArguments();
110117

111-
return $this->addComments(
118+
$translation = $this->addComments(
112119
$function,
113120
$this->saveTranslation($domain, $context, $original, $plural)
114121
);
122+
return $this->addFlags($function, $translation);
115123
}
116124

117125
abstract protected function addComments(ParsedFunction $function, ?Translation $translation): ?Translation;
118126

127+
abstract protected function addFlags(ParsedFunction $function, ?Translation $translation): ?Translation;
128+
119129
abstract protected function checkFunction(ParsedFunction $function, int $minLength): bool;
120130

121131
abstract protected function saveTranslation(

src/Scanner/ParsedFunction.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ final class ParsedFunction
1414
private $lastLine;
1515
private $arguments = [];
1616
private $comments = [];
17+
private $flags = [];
1718

1819
public function __construct(string $name, string $filename, int $line, int $lastLine = null)
1920
{
@@ -37,6 +38,7 @@ public function toArray(): array
3738
'lastLine' => $this->lastLine,
3839
'arguments' => $this->arguments,
3940
'comments' => $this->comments,
41+
'flags' => $this->flags,
4042
];
4143
}
4244

@@ -75,6 +77,11 @@ public function getComments(): array
7577
return $this->comments;
7678
}
7779

80+
public function getFlags(): array
81+
{
82+
return $this->flags;
83+
}
84+
7885
public function addArgument($argument = null): self
7986
{
8087
$this->arguments[] = $argument;
@@ -88,4 +95,11 @@ public function addComment(string $comment): self
8895

8996
return $this;
9097
}
98+
99+
public function addFlag(string $flag): self
100+
{
101+
$this->flags[] = $flag;
102+
103+
return $this;
104+
}
91105
}

tests/ParsedFunctionsAndCommentsTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,8 @@ public function testParsedFunction()
2828

2929
$function->addComment('This is other comment');
3030
$this->assertSame(['This is a comment', 'This is other comment'], $function->getComments());
31+
32+
$function->addFlag('php-format');
33+
$this->assertSame(['php-format'], $function->getFlags());
3134
}
3235
}

0 commit comments

Comments
 (0)