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

Add Getters & Setters metric #267

Merged
merged 180 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
180 commits
Select commit Hold shift + click to select a range
8553bde
Add getset metric
padjal Apr 4, 2024
1eb57f7
Fix getset metric
padjal Apr 4, 2024
7d1cddb
Remove method name from results
padjal Apr 6, 2024
094aae8
Code refactoring
padjal Apr 6, 2024
4c432b8
Fix metric output so that it gives more information
padjal Apr 21, 2024
a297c7e
Fix linting errors
padjal Apr 21, 2024
dcc38ad
Fix other linting errors
padjal Apr 21, 2024
8721b7f
Fix all linting errors
padjal Apr 21, 2024
0cbc520
Fix metric
padjal Apr 21, 2024
633a641
Add copyright to metric
padjal Apr 21, 2024
66c60ff
Change test description
padjal May 3, 2024
70c32e2
midx
yegor256 Apr 5, 2024
02319d1
halstead
yegor256 Apr 5, 2024
79507ac
layout
yegor256 Apr 5, 2024
cef6956
lint
yegor256 Apr 5, 2024
312ae15
no latexmk
yegor256 Apr 5, 2024
908abbf
float.sh must print exactly three digits after the dot
LaithAlebrahim Apr 5, 2024
3721341
fix names metrics bug
LaithAlebrahim Apr 5, 2024
7c33667
fix shellcheck issue with float.sh
LaithAlebrahim Apr 5, 2024
108b369
test layout
yegor256 Apr 5, 2024
1e12bba
better logs
yegor256 Apr 6, 2024
5aec2ba
Added the new restiction for metrices name
LaithAlebrahim Apr 5, 2024
d669325
Fix code format
LaithAlebrahim Apr 6, 2024
d14e340
get rid of redundant varibles
LaithAlebrahim Apr 7, 2024
e8078c6
added test for irca
zaqbez39me Apr 15, 2024
eca5918
fixed the way of disabling test for irca
zaqbez39me Apr 16, 2024
1c79855
disabled shellcheck SC2317
zaqbez39me Apr 16, 2024
f6f5532
added execution permission in git index for test-ir.sh
zaqbez39me Apr 16, 2024
3942b40
Revert "disabled shellcheck SC2317"
zaqbez39me Apr 16, 2024
88f128e
added execution permission in git index for test-irca.sh
zaqbez39me Apr 16, 2024
3430eac
disabled shellcheck SC2317
zaqbez39me Apr 16, 2024
3b006e3
added empty irca.sh
zaqbez39me Apr 16, 2024
01f7f32
made irca.sh executable
zaqbez39me Apr 16, 2024
74c8946
added pipefail to irca.sh
zaqbez39me Apr 16, 2024
f5b18df
added stub to irca.sh
zaqbez39me Apr 16, 2024
40f2635
Fixed test-measure.sh to check suffixes properly and be quiet
zaqbez39me Apr 8, 2024
daa8adc
sonar badge
yegor256 Apr 9, 2024
006e198
Refactor If statements in metrics/ast.py to have same branch.
tanmaysharma2001 Apr 9, 2024
4547788
link
yegor256 Apr 14, 2024
0875e5b
ethical
yegor256 Apr 15, 2024
2db9b44
#276 assert-tool introduced
yegor256 Apr 15, 2024
03c08e8
typo
yegor256 Apr 15, 2024
6d7be31
more asserts
yegor256 Apr 15, 2024
6bfc62d
doc(#227): fetch topics too, document /steps in steps/README.md
Apr 15, 2024
ae17aae
doc: more pretty
Apr 15, 2024
ba0113e
feat(#227): topics csv arg
Apr 15, 2024
7ad33f1
doc: clean for mdlint
Apr 15, 2024
5e2cfe1
doc: heading at ## level
Apr 15, 2024
1e0a387
doc: heading at ## level
Apr 15, 2024
f3727e8
doc: typo
Apr 15, 2024
ff83af5
doc: one heading
Apr 15, 2024
c129246
md
yegor256 Apr 16, 2024
f961c82
changed stub
zaqbez39me Apr 16, 2024
ff7b9dd
added tolerance to comparison of expected and actual number of metrics
zaqbez39me Apr 17, 2024
5e9e9dc
pdd
yegor256 Apr 19, 2024
983b3ab
todo removed
yegor256 Apr 19, 2024
a284a02
readme
yegor256 Apr 19, 2024
08c346a
typos
yegor256 Apr 19, 2024
cb50db7
test fixed
yegor256 Apr 20, 2024
ab7a564
#285 better log
yegor256 Apr 21, 2024
6ffff5b
#285 raf test fixed
yegor256 Apr 21, 2024
bcb2faf
Add delete wrongly encoded files
padjal Apr 18, 2024
34ee757
Add tests to delete wrong encoding filter
padjal Apr 19, 2024
5cacf0b
Fix python lint error
padjal Apr 19, 2024
bfdab0b
Fix unused imports and over-indented code
padjal Apr 19, 2024
11ec866
Lint: Remove whitespace from blank line
padjal Apr 19, 2024
36cb532
Add executions script for wrong encoding filter
padjal Apr 19, 2024
b59eb29
Add test for execution script
padjal Apr 19, 2024
21eaee3
Conform to lint suggestion: consider-using-in
padjal Apr 20, 2024
b97ffe7
Add one more file with wrong encoding to the test-filter script.
padjal Apr 21, 2024
ebc013e
typos
yegor256 Apr 21, 2024
4450b2f
tests fixed
yegor256 Apr 21, 2024
161cf3f
add license filter to discover-repos.rb
Orillio Apr 18, 2024
995cf04
fix linter
Orillio Apr 18, 2024
2884148
remove redundant key
Orillio Apr 19, 2024
f359eb1
refactor variable name
Orillio Apr 20, 2024
f88beed
add skipping repo log
Orillio Apr 21, 2024
c348584
fix test-raf on mac os
IlyaPechersky Apr 24, 2024
218b644
Update dependency mypy to v1.10.0
renovate[bot] Apr 24, 2024
0fac0c2
Hits of code metric
IlnurHA Mar 5, 2024
49c43df
Update hoc.py
IlnurHA Mar 5, 2024
b221df2
Rebase problem
IlnurHA Mar 5, 2024
3ac05b8
Test for hoc.sh
IlnurHA Mar 5, 2024
8a70514
Fixed test and metric
IlnurHA Mar 5, 2024
3340f1c
Updated HOC
IlnurHA Mar 25, 2024
ea2661f
Updated tests for hoc
IlnurHA Mar 25, 2024
60ea0aa
Fix Pipeline error
IlnurHA Mar 25, 2024
c455615
Fix access to be executable for make pipeline
IlnurHA Mar 25, 2024
f317494
Fixed path for script
IlnurHA Mar 26, 2024
4804083
Output for hoc to be parsable
IlnurHA Mar 26, 2024
5cfddfa
Fix Test for HOC and comment unused variable
IlnurHA Mar 28, 2024
8cfe20e
Uncomment temp; It is actually used
IlnurHA Mar 28, 2024
fd1bdde
remove class name from metric and test
IlnurHA Apr 25, 2024
7fb5e2b
Update actions/checkout digest to 8459bc0
renovate[bot] Apr 24, 2024
1c1e15e
fix
yegor256 Apr 26, 2024
88076ad
added test for irloc
zaqbez39me Apr 15, 2024
85e5e57
fixed namings of metric files in test-irloc.sh
zaqbez39me Apr 15, 2024
c6a6b3b
fixed the way of test disabling
zaqbez39me Apr 16, 2024
bc182b7
added a stub for irloc.sh
zaqbez39me Apr 17, 2024
e28a896
disabled SC2317 in test-irloc.sh because test is not used for now
zaqbez39me Apr 17, 2024
a6f985b
added executional permission for test-irloc.sh
zaqbez39me Apr 17, 2024
26c9c9c
updated stub with initial content
zaqbez39me Apr 17, 2024
9d500c0
added executional permission for irloc.sh
zaqbez39me Apr 17, 2024
6a92fea
fixed the expected behaviour
zaqbez39me Apr 24, 2024
3703439
fixed the format of todos
zaqbez39me Apr 24, 2024
9164fe2
merged two TODOs in one
zaqbez39me Apr 25, 2024
1736ece
fixed merge of TODOs
zaqbez39me Apr 25, 2024
f89e4e8
removed TOOO from irloc.sh
zaqbez39me Apr 25, 2024
d20a6f0
created test for irc
zaqbez39me Apr 15, 2024
62ed000
refactored test by splitting it on smaller tests
zaqbez39me Apr 15, 2024
ca29e2f
fixed the way of disabling test for irc
zaqbez39me Apr 16, 2024
db3a81f
initialized a stub for future irc metric
zaqbez39me Apr 17, 2024
f61c292
added executional permission for test-irc.sh
zaqbez39me Apr 17, 2024
a11fea9
disabled SC2317 for test-irc.sh because test is disabled for now
zaqbez39me Apr 17, 2024
d1a4e70
added executional permission for irc.sh
zaqbez39me Apr 17, 2024
4d13e7c
changed expected behaviour for scripts
zaqbez39me Apr 24, 2024
806a760
fixed the format of todos
zaqbez39me Apr 24, 2024
06d7504
merged two TODOs in one
zaqbez39me Apr 25, 2024
ae807d7
removed TOOO from irc.sh
zaqbez39me Apr 25, 2024
3e556b7
implemented test for ir feature
zaqbez39me Mar 31, 2024
9b13da7
added all the tests to test file
zaqbez39me Mar 31, 2024
81a1df8
fixed the way of disabling test for ir
zaqbez39me Apr 16, 2024
7956da5
disabled shellcheck SC2317
zaqbez39me Apr 16, 2024
ef4fabd
added execution permission in git index for test-ir.sh
zaqbez39me Apr 16, 2024
a1f825b
added empty ir.sh
zaqbez39me Apr 16, 2024
4a616c2
added excutional permition for ir.sh and stub in it
zaqbez39me Apr 16, 2024
e497398
updated the stub for ir.sh
zaqbez39me Apr 17, 2024
3d2b1a3
changed expected bahviour for test-ir.sh
zaqbez39me Apr 24, 2024
963c59e
fixed todo
zaqbez39me Apr 24, 2024
e123006
fixed todo in ir.sh
zaqbez39me Apr 24, 2024
dc71733
merged two TODOs in one
zaqbez39me Apr 25, 2024
c0cbafa
removed TOOO from ir.sh
zaqbez39me Apr 25, 2024
4df48bd
fix missing
yegor256 Apr 27, 2024
5992eca
sort
yegor256 Apr 27, 2024
0aca296
is-linux.sh
yegor256 Apr 27, 2024
3558d24
sudo
yegor256 Apr 27, 2024
c97785b
show log
yegor256 Apr 27, 2024
6fe0aad
ln
yegor256 Apr 27, 2024
c0b2d9f
fmt
yegor256 Apr 27, 2024
dd0d0f2
gem check
yegor256 Apr 27, 2024
600c953
log
yegor256 Apr 27, 2024
01881e6
polish
yegor256 Apr 27, 2024
520411d
exit
yegor256 Apr 27, 2024
fc2de6f
print version
yegor256 Apr 27, 2024
ea9be85
typo
yegor256 Apr 27, 2024
56de2ae
action lint
yegor256 Apr 27, 2024
f500b08
sudo
yegor256 Apr 27, 2024
caa8df0
hoc renamed
yegor256 Apr 27, 2024
c996c5f
Update ubuntu Docker tag to v24
renovate[bot] Apr 26, 2024
e156cf5
#166 pylint up
yegor256 Apr 27, 2024
68837a9
#166 pylint up
yegor256 Apr 27, 2024
aea2053
no templates
yegor256 May 2, 2024
18b97de
fix(#294): description, topics escaped
h1alexbel Apr 30, 2024
3695506
fix(#294): single quotes
h1alexbel Apr 30, 2024
5f91609
fix(#294): space
h1alexbel Apr 30, 2024
549bd2f
fix(#294): arrays
h1alexbel Apr 30, 2024
797cba0
chore: yamllint
h1alexbel Apr 30, 2024
ee335a3
chore: linlength disable on run line
h1alexbel Apr 30, 2024
3ae08a2
fixed error from linter
IlnurHA May 3, 2024
fc899df
fix report.bib for linter
IlnurHA May 3, 2024
47d9126
polish
yegor256 May 3, 2024
8ac196a
Relative File Volatility by Hits metric
IlnurHA May 3, 2024
091893a
Test for rfvh
IlnurHA May 3, 2024
e280cee
Add pipefail
IlnurHA May 3, 2024
9c423f2
Change for test and metric to be executable
IlnurHA May 3, 2024
30442cf
Fixed naming for RFVH
IlnurHA May 3, 2024
ad73b67
change equality to inequality
IlnurHA May 3, 2024
107c02e
Relative File Volatility by Commits
IlnurHA May 3, 2024
eba5d7d
added pipefail
IlnurHA May 3, 2024
82010fc
Test for rfvc
IlnurHA May 3, 2024
d6e087a
make rfvc metric and test to be executable
IlnurHA May 3, 2024
4dfb6ac
Added several commits for test
IlnurHA May 3, 2024
463e8eb
Fixed naming for metric output
IlnurHA May 3, 2024
14b6d30
Add copyright protection
padjal May 3, 2024
1e33d18
Check search term to full string
padjal May 6, 2024
ffa5edb
Merge branch 'master' into get-set
padjal May 6, 2024
bfca9f5
Add copyright to test
padjal May 6, 2024
33eefdc
Fix test output
padjal May 6, 2024
85914de
Lint fix for python files
padjal May 6, 2024
1f58d6a
Change metric
padjal May 7, 2024
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
70 changes: 70 additions & 0 deletions metrics/getset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env python3
import sys
from javalang import tree, parse

sys.setrecursionlimit(10000)

def is_getter_or_setter(method):
"""Figures out whether a method is a getter or a setter."""
if isinstance(method, tree.MethodDeclaration):
if method.name.startswith("get") and len(method.parameters) == 0:
return "getter"
elif method.name.startswith("set") and len(method.parameters) == 1:
return "setter"
return None

def count_branches(node):
"""Counts the number of branches in a method."""
count = 0
if isinstance(node, (tree.IfStatement, tree.WhileStatement, tree.ForStatement, tree.DoStatement, tree.SwitchStatement, tree.CatchClause)):
count = 1
return count

def analyze_method(method):
"""Analyzes the complexity of the method."""
method_type = is_getter_or_setter(method)
if not method_type:
return None

complexity = 0
branches = 0
for _, node in method.filter(tree.Statement):
branches += count_branches(node)

# print(method_type)
# print(method.name[3:].lower())

if method_type == "setter":
# Increase complexity for each additional variable assignment
if isinstance(node, tree.Assignment) and node.expressionl != method.parameters[0].name:
complexity += 1

elif method_type == "getter":
# print('Expression: ', node.expression)
# print(node.expression.selectors[0].member)

# Increase complexity for each statement not related to the returned variable
if not (isinstance(node, tree.ReturnStatement) and node.expression.selectors[0].member == method.name[3:].lower()):
complexity += 1

# Increase complexity for branching
complexity += branches

return method_type, complexity, branches

if __name__ == '__main__':
JAVA = sys.argv[1]
METRICS = sys.argv[2]
with open(JAVA, encoding='utf-8', errors='ignore') as f:
try:
ast = parse.parse(f.read())
for _, node in ast.filter(tree.MethodDeclaration):
result = analyze_method(node)
if result:
method_type, complexity, branches = result
with open(METRICS, 'a', encoding='utf-8') as m:
# m.write(f'{method_type} {complexity} {branches} Branches of {node.name}\n')
m.write(f'{method_type} {complexity} {branches}\n')
except FileNotFoundError as exception:
message = f"{type(exception).__name__} {str(exception)}: {JAVA}"
sys.exit(message)
69 changes: 69 additions & 0 deletions tests/metrics/test-getset.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env bash
# Test script for analyzing Java getter and setter methods and their complexities

set -e
set -o pipefail

# Setup temporary workspace
# temp=$(mktemp -d)
# stdout="${temp}/stdout.txt"

temp=$1
stdout=$2

# Location of the Python script
script_location="${LOCAL}/metrics/getset.py"

# echo "${script_location}"
# echo "${temp}"
padjal marked this conversation as resolved.
Show resolved Hide resolved

{
# Create a simple Java file with getter and setter methods
java_file="${temp}/Person.java"
echo "public class Person {
private String name;
private int age;

public String getName() {
return this.name;
}

padjal marked this conversation as resolved.
Show resolved Hide resolved
public void setName(String name) {
this.name = name;
}

public int getAge() {
return this.age;
}

public void setAge(int age) {
this.age = age;
}

public void nonAccessorMethod() {
if (age > 18) {
System.out.println(\"Adult\");
}
}
}" > "${java_file}"

metrics_file="${temp}/metrics.txt"
# echo "${metrics_file}"
"${script_location}" "${java_file}" "${metrics_file}"
cat "${metrics_file}"

# echo "${}"

# Assertions: Check for expected output related to getter, setter, and branches
# grep "getter 0 0 Branches of getName" "${metrics_file}"
# grep "setter 0 0 Branches of setName" "${metrics_file}"
# grep "getter 0 0 Branches of getAge" "${metrics_file}"
# grep "setter 0 0 Branches of setAge" "${metrics_file}"
grep "getter 0 0" "${metrics_file}"
padjal marked this conversation as resolved.
Show resolved Hide resolved
grep "setter 0 0" "${metrics_file}"
grep "getter 0 0" "${metrics_file}"
grep "setter 0 0" "${metrics_file}"
grep "none 1 1 Branches of nonAccessorMethod" "${metrics_file}" || true # This line is expected to fail since your script currently doesn't handle non-accessor methods
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@padjal the output doesn't look correct, please read: https://github.com/yegor256/cam/blob/master/metrics/README.md

} > "${stdout}" 2>&1

echo "👍 Tests passed successfully"
padjal marked this conversation as resolved.
Show resolved Hide resolved
Loading