Skip to content

Commit d4fb10d

Browse files
isha689childish-sambino
andauthored
fix: cleanup php unused use statements (#320)
<!-- We appreciate the effort for this pull request but before that please make sure you read the contribution guidelines, then fill out the blanks below. Please format the PR title appropriately based on the type of change: <type>[!]: <description> Where <type> is one of: docs, chore, feat, fix, test, misc. Add a '!' after the type for breaking changes (e.g. feat!: new breaking feature). **All third-party contributors acknowledge that any contributions they provide will be made under the same open-source license that the open-source project is provided under.** Please enter each Issue number you are resolving in your PR after one of the following words [Fixes, Closes, Resolves]. This will auto-link these issues and close them when this PR is merged! e.g. Fixes #1 Closes #2 --> # Fixes [DII-702](https://issues.corp.twilio.com/browse/DII-702) Cleans unused "use" statements from php code files. ### Checklist - [x] I acknowledge that all my contributions will be made under the project's license - [ ] Run `make test-docker` - [ ] Verify affected language: - [ ] Generate [twilio-go](https://github.com/twilio/twilio-go) from our [OpenAPI specification](https://github.com/twilio/twilio-oai) using the [build_twilio_go.py](./examples/build_twilio_go.py) using `python examples/build_twilio_go.py path/to/twilio-oai/spec/yaml path/to/twilio-go` and inspect the diff - [ ] Run `make test` in `twilio-go` - [ ] Create a pull request in `twilio-go` - [ ] Provide a link below to the pull request - [ ] I have made a material change to the repo (functionality, testing, spelling, grammar) - [ ] I have read the [Contribution Guidelines](https://github.com/twilio/twilio-oai-generator/blob/main/CONTRIBUTING.md) and my PR follows them - [ ] I have titled the PR appropriately - [ ] I have updated my branch with the main branch - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] I have added the necessary documentation about the functionality in the appropriate .md file - [ ] I have added inline documentation to the code I modified If you have questions, please create a GitHub Issue in this repository. Co-authored-by: childish-sambino <sharrison@twilio.com>
1 parent ce47b78 commit d4fb10d

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

scripts/build_twilio_library.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
from pathlib import Path
55
from typing import Tuple
66

7-
from clean_java_imports import remove_unused_imports
8-
7+
from clean_unused_imports import remove_unused_imports
98
'''
109
Subdirectories map for maintaining directory
1110
structures specific to language style.
@@ -15,7 +14,7 @@
1514
'csharp': 'Rest',
1615
'php': 'Rest'
1716
}
18-
17+
CLEANUP_IMPORT_LANGUAGES = ['java', 'php']
1918

2019
def build(openapi_spec_path: str, output_path: str, language: str) -> None:
2120
if os.path.isfile(openapi_spec_path):
@@ -33,8 +32,8 @@ def generate(openapi_spec_path: str, output_path: str, language: str, domain: st
3332
sub_dir = subdirectories.get(language, 'rest')
3433
output_path = os.path.join(output_path, sub_dir)
3534
run_openapi_generator(parent_dir, language, output_path, full_path)
36-
if language == 'java':
37-
remove_unused_imports(output_path, 'java')
35+
if language in CLEANUP_IMPORT_LANGUAGES:
36+
remove_unused_imports(output_path, language)
3837

3938

4039
def run_openapi_generator(parent_dir: str, language: str, output_path: str, full_path: str) -> None:

scripts/clean_java_imports.py renamed to scripts/clean_unused_imports.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
import os
33
import re
44

5-
IMPORT_RE = re.compile(r'^\s*import\s+[\w\.]+\.(\w+)\s*;\s*(?://.*)?$')
6-
75
LANGUAGE_REGEX_MAP = {
8-
"java": "*.java"
6+
"java": {
7+
"pattern": "*.java",
8+
"regex": re.compile(r'^\s*import\s+[\w\.]+\.(\w+)\s*;\s*(?://.*)?$')
9+
},
10+
"php": {
11+
"pattern": "*.php",
12+
"regex": re.compile(r'^\s*use\s+[\w\\]+\\(\w+)\s*\w*\s*(\w*);\s*(?://.*)?$')
13+
}
914
}
1015

1116

@@ -16,15 +21,19 @@ def locate(pattern, root=os.curdir):
1621

1722

1823
def remove_unused_imports(root_dir, language):
19-
for filename in locate(LANGUAGE_REGEX_MAP[language], root_dir):
24+
for filename in locate(LANGUAGE_REGEX_MAP[language]["pattern"], root_dir):
2025
import_lines = {}
2126
other_lines = []
2227
with open(filename) as f:
2328
all_lines = f.readlines()
2429
for n, line in enumerate(all_lines):
30+
IMPORT_RE=LANGUAGE_REGEX_MAP[language]["regex"]
2531
m = IMPORT_RE.match(line)
2632
if m:
27-
import_lines[n] = m.group(1)
33+
if " as " in line:
34+
import_lines[n] = m.group(2)
35+
else:
36+
import_lines[n] = m.group(1)
2837
else:
2938
other_lines.append(line)
3039
other_code = ''.join(other_lines)

0 commit comments

Comments
 (0)