Total Complexity | 4 |
Total Lines | 54 |
Duplicated Lines | 85.19 % |
Coverage | 60% |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | # ---------------------------------------------------------------------------------------------------------------------- |
||
2 | 1 | from typing import Optional |
|
3 | |||
4 | 1 | from antlr4.Token import CommonToken |
|
5 | 1 | from cleo.io.io import IO |
|
6 | |||
7 | |||
8 | 1 | View Code Duplication | class SDocVisitor: |
|
|||
9 | """ |
||
10 | Parent visitor for SDoc level 1 & 2. |
||
11 | """ |
||
12 | |||
13 | # ------------------------------------------------------------------------------------------------------------------ |
||
14 | 1 | def __init__(self, io: IO): |
|
15 | """ |
||
16 | Object constructor. |
||
17 | """ |
||
18 | |||
19 | 1 | self._io: IO = io |
|
20 | """ |
||
21 | Styled output formatter. |
||
22 | """ |
||
23 | |||
24 | 1 | self._errors: int = 0 |
|
25 | 1 | """ |
|
26 | The error count. |
||
27 | """ |
||
28 | |||
29 | # ------------------------------------------------------------------------------------------------------------------ |
||
30 | 1 | @property |
|
31 | 1 | def errors(self) -> int: |
|
32 | """ |
||
33 | Getter for the error count. |
||
34 | """ |
||
35 | 1 | return self._errors |
|
36 | |||
37 | # ------------------------------------------------------------------------------------------------------------------ |
||
38 | 1 | def _error(self, message: str, token: Optional[CommonToken] = None) -> None: |
|
39 | """ |
||
40 | Logs an error. |
||
41 | |||
42 | :param str message: The error message.This message will be appended with 'at filename:line.column' ot the token. |
||
43 | :param antlr4.Token.CommonToken token: The token where the error occurred. |
||
44 | """ |
||
45 | self._errors += 1 |
||
46 | |||
47 | filename = token.getInputStream().fileName # Replace fileName with get_source_name() when implemented in ANTLR. |
||
48 | line_number = token.line |
||
49 | column_number = token.column + 1 |
||
50 | messages = [message] |
||
51 | if token: |
||
52 | messages.append('Position: {0!s}:{1:d}.{2:d}'.format(filename, line_number, column_number)) |
||
53 | self._io.write_error(messages) |
||
54 | |||
56 |