-
Notifications
You must be signed in to change notification settings - Fork 982
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
feat: ck printer #1895
Merged
Merged
feat: ck printer #1895
Changes from 43 commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
201b4df
feat: halstead printer
devtooligan 7eb270a
feat: halstead printer
devtooligan 34a343e
feat: ck printer
devtooligan 16b5726
feat: martin printer
devtooligan d682232
chore: add to scripts/ci_test_printers.sh
devtooligan 6a0add0
Merge branch 'dev' into halstead
devtooligan 1fa616b
Merge branch 'dev' into ck-printer
devtooligan 314364e
chore: remove comments
devtooligan 134dd90
Merge branch 'dev' into martin-printer
devtooligan 975da91
chore: add type
devtooligan 6f280c1
chore: pylint
devtooligan 14c9761
feat: add CBO
devtooligan 45f353b
Merge branch 'dev' into martin-printer
devtooligan fa22c34
chore: update label
devtooligan 0fb6597
Merge branch 'martin-printer' into ck-printer
devtooligan 3791467
docs: fix docstring
devtooligan c3a674a
chore: black
devtooligan a826bc3
Merge branch 'dev' into halstead
devtooligan c175d52
Merge branch 'dev' into halstead
devtooligan 06e218c
refactor: prefer custom classes to dicts
devtooligan db5ec71
chore: move halstead utilities to utils folder
devtooligan 0fb6e42
chore: lint
devtooligan 46c6177
Merge branch 'dev' into halstead
devtooligan 8f831ad
fix: 'type' object is not subscriptable
devtooligan 61e3076
chore: lint
devtooligan fb25cbb
refactor: initial
devtooligan ce76d62
Merge branch 'halstead' into martin-printer
devtooligan 8d2e7c4
refactor: prefer custom classes to dicts
devtooligan c787fb4
chore: move Martin logic to utils
devtooligan 2e99e49
chore: lint
devtooligan 8c51e47
Update scripts/ci_test_printers.sh
devtooligan 5699349
fix: typo
devtooligan 961a678
Merge branch 'halstead' into martin-printer
devtooligan 4a3ab0a
fix: add martin printer to testing printer list
devtooligan 524b755
Merge branch 'martin-printer' into ck-printer
devtooligan 42cd6e0
fix: account for case w no functions
devtooligan 251dee0
Merge branch 'halstead' into martin-printer
devtooligan 6552ba0
Merge branch 'martin-printer' into ck-printer
devtooligan 8d483ed
fix: external calls
devtooligan 5c6cfb8
Merge branch 'martin-printer' into ck-printer
devtooligan 6843d03
fix: external calls
devtooligan 8a34cd4
Merge branch 'martin-printer' into ck-printer
devtooligan a35dff3
refactor: ck
devtooligan 628f723
Merge branch 'dev' into ck-printer
montyly 4053c9b
minor improvements
montyly c8bd72e
Fix circular dep
montyly bcbe4ff
Update ci_test_printers.sh
montyly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
""" | ||
CK Metrics are a suite of six software metrics proposed by Chidamber and Kemerer in 1994. | ||
These metrics are used to measure the complexity of a class. | ||
https://en.wikipedia.org/wiki/Programming_complexity | ||
|
||
- Response For a Class (RFC) is a metric that measures the number of unique method calls within a class. | ||
- Number of Children (NOC) is a metric that measures the number of children a class has. | ||
- Depth of Inheritance Tree (DIT) is a metric that measures the number of parent classes a class has. | ||
- Coupling Between Object Classes (CBO) is a metric that measures the number of classes a class is coupled to. | ||
|
||
Not implemented: | ||
- Lack of Cohesion of Methods (LCOM) is a metric that measures the lack of cohesion in methods. | ||
- Weighted Methods per Class (WMC) is a metric that measures the complexity of a class. | ||
|
||
During the calculation of the metrics above, there are a number of other intermediate metrics that are calculated. | ||
These are also included in the output: | ||
- State variables: total number of state variables | ||
- Constants: total number of constants | ||
- Immutables: total number of immutables | ||
- Public: total number of public functions | ||
- External: total number of external functions | ||
- Internal: total number of internal functions | ||
- Private: total number of private functions | ||
- Mutating: total number of state mutating functions | ||
- View: total number of view functions | ||
- Pure: total number of pure functions | ||
- External mutating: total number of external mutating functions | ||
- No auth or onlyOwner: total number of functions without auth or onlyOwner modifiers | ||
- No modifiers: total number of functions without modifiers | ||
- Ext calls: total number of external calls | ||
|
||
""" | ||
from slither.printers.abstract_printer import AbstractPrinter | ||
from slither.utils.ck import CKMetrics | ||
|
||
|
||
class CK(AbstractPrinter): | ||
ARGUMENT = "ck" | ||
HELP = "Chidamber and Kemerer (CK) complexity metrics and related function attributes" | ||
|
||
WIKI = "https://github.com/trailofbits/slither/wiki/Printer-documentation#ck" | ||
|
||
def output(self, _filename): | ||
if len(self.contracts) == 0: | ||
return self.generate_output("No contract found") | ||
|
||
ck = CKMetrics(self.contracts) | ||
|
||
res = self.generate_output(ck.full_text) | ||
res.add_pretty_table(ck.auxiliary1.pretty_table, ck.auxiliary1.title) | ||
res.add_pretty_table(ck.auxiliary2.pretty_table, ck.auxiliary2.title) | ||
res.add_pretty_table(ck.auxiliary3.pretty_table, ck.auxiliary3.title) | ||
res.add_pretty_table(ck.auxiliary4.pretty_table, ck.auxiliary4.title) | ||
res.add_pretty_table(ck.core.pretty_table, ck.core.title) | ||
self.info(ck.full_text) | ||
|
||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
""" | ||
Halstead complexity metrics | ||
https://en.wikipedia.org/wiki/Halstead_complexity_measures | ||
|
||
12 metrics based on the number of unique operators and operands: | ||
|
||
Core metrics: | ||
n1 = the number of distinct operators | ||
n2 = the number of distinct operands | ||
N1 = the total number of operators | ||
N2 = the total number of operands | ||
|
||
Extended metrics1: | ||
n = n1 + n2 # Program vocabulary | ||
N = N1 + N2 # Program length | ||
S = n1 * log2(n1) + n2 * log2(n2) # Estimated program length | ||
V = N * log2(n) # Volume | ||
|
||
Extended metrics2: | ||
D = (n1 / 2) * (N2 / n2) # Difficulty | ||
E = D * V # Effort | ||
T = E / 18 seconds # Time required to program | ||
B = (E^(2/3)) / 3000 # Number of delivered bugs | ||
|
||
""" | ||
from slither.printers.abstract_printer import AbstractPrinter | ||
from slither.utils.halstead import HalsteadMetrics | ||
|
||
|
||
class Halstead(AbstractPrinter): | ||
ARGUMENT = "halstead" | ||
HELP = "Computes the Halstead complexity metrics for each contract" | ||
|
||
WIKI = "https://github.com/trailofbits/slither/wiki/Printer-documentation#halstead" | ||
|
||
def output(self, _filename): | ||
if len(self.contracts) == 0: | ||
return self.generate_output("No contract found") | ||
|
||
halstead = HalsteadMetrics(self.contracts) | ||
|
||
res = self.generate_output(halstead.full_text) | ||
res.add_pretty_table(halstead.core.pretty_table, halstead.core.title) | ||
res.add_pretty_table(halstead.extended1.pretty_table, halstead.extended1.title) | ||
res.add_pretty_table(halstead.extended2.pretty_table, halstead.extended2.title) | ||
self.info(halstead.full_text) | ||
|
||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
""" | ||
Robert "Uncle Bob" Martin - Agile software metrics | ||
https://en.wikipedia.org/wiki/Software_package_metrics | ||
|
||
Efferent Coupling (Ce): Number of contracts that the contract depends on | ||
Afferent Coupling (Ca): Number of contracts that depend on a contract | ||
Instability (I): Ratio of efferent coupling to total coupling (Ce / (Ce + Ca)) | ||
Abstractness (A): Number of abstract contracts / total number of contracts | ||
Distance from the Main Sequence (D): abs(A + I - 1) | ||
|
||
""" | ||
from slither.printers.abstract_printer import AbstractPrinter | ||
from slither.utils.martin import MartinMetrics | ||
|
||
|
||
class Martin(AbstractPrinter): | ||
ARGUMENT = "martin" | ||
HELP = "Martin agile software metrics (Ca, Ce, I, A, D)" | ||
|
||
WIKI = "https://github.com/trailofbits/slither/wiki/Printer-documentation#martin" | ||
|
||
def output(self, _filename): | ||
if len(self.contracts) == 0: | ||
return self.generate_output("No contract found") | ||
|
||
martin = MartinMetrics(self.contracts) | ||
|
||
res = self.generate_output(martin.full_text) | ||
res.add_pretty_table(martin.core.pretty_table, martin.core.title) | ||
self.info(martin.full_text) | ||
return res |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add the return types for all the functions? (use
-> None
if the function returns nothing)