Skip to content

Commit c7a8e40

Browse files
authored
Refactor classes. (#89)
1 parent 3d85ba8 commit c7a8e40

File tree

19 files changed

+425
-322
lines changed

19 files changed

+425
-322
lines changed

.clang-format

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
---
2+
Language: Cpp
3+
# BasedOnStyle: LLVM
4+
AccessModifierOffset: -2
5+
AlignAfterOpenBracket: Align
6+
AlignConsecutiveMacros: false
7+
AlignConsecutiveAssignments: false
8+
AlignConsecutiveDeclarations: false
9+
AlignEscapedNewlines: Right
10+
AlignOperands: true
11+
AlignTrailingComments: true
12+
AllowAllArgumentsOnNextLine: true
13+
AllowAllConstructorInitializersOnNextLine: true
14+
AllowAllParametersOfDeclarationOnNextLine: true
15+
AllowShortBlocksOnASingleLine: Never
16+
AllowShortCaseLabelsOnASingleLine: false
17+
AllowShortFunctionsOnASingleLine: Empty
18+
AllowShortLambdasOnASingleLine: All
19+
AllowShortIfStatementsOnASingleLine: Never
20+
AllowShortLoopsOnASingleLine: false
21+
AlwaysBreakAfterDefinitionReturnType: None
22+
AlwaysBreakAfterReturnType: None
23+
AlwaysBreakBeforeMultilineStrings: false
24+
AlwaysBreakTemplateDeclarations: MultiLine
25+
BinPackArguments: true
26+
BinPackParameters: true
27+
BraceWrapping:
28+
AfterCaseLabel: false
29+
AfterClass: false
30+
AfterControlStatement: false
31+
AfterEnum: false
32+
AfterFunction: false
33+
AfterNamespace: false
34+
AfterObjCDeclaration: false
35+
AfterStruct: false
36+
AfterUnion: false
37+
AfterExternBlock: false
38+
BeforeCatch: false
39+
BeforeElse: false
40+
IndentBraces: false
41+
SplitEmptyFunction: true
42+
SplitEmptyRecord: true
43+
SplitEmptyNamespace: true
44+
BreakBeforeBinaryOperators: None
45+
BreakBeforeBraces: Attach
46+
BreakBeforeInheritanceComma: false
47+
BreakInheritanceList: BeforeColon
48+
BreakBeforeTernaryOperators: true
49+
BreakConstructorInitializersBeforeComma: false
50+
BreakConstructorInitializers: BeforeColon
51+
BreakAfterJavaFieldAnnotations: false
52+
BreakStringLiterals: true
53+
ColumnLimit: 80
54+
CommentPragmas: '^ IWYU pragma:'
55+
CompactNamespaces: false
56+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
57+
ConstructorInitializerIndentWidth: 4
58+
ContinuationIndentWidth: 4
59+
Cpp11BracedListStyle: true
60+
DeriveLineEnding: true
61+
DerivePointerAlignment: false
62+
DisableFormat: false
63+
ExperimentalAutoDetectBinPacking: false
64+
FixNamespaceComments: true
65+
ForEachMacros:
66+
- foreach
67+
- Q_FOREACH
68+
- BOOST_FOREACH
69+
IncludeBlocks: Preserve
70+
IncludeCategories:
71+
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
72+
Priority: 2
73+
SortPriority: 0
74+
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
75+
Priority: 3
76+
SortPriority: 0
77+
- Regex: '.*'
78+
Priority: 1
79+
SortPriority: 0
80+
IncludeIsMainRegex: '(Test)?$'
81+
IncludeIsMainSourceRegex: ''
82+
IndentCaseLabels: false
83+
IndentGotoLabels: true
84+
IndentPPDirectives: None
85+
IndentWidth: 4
86+
IndentWrappedFunctionNames: false
87+
JavaScriptQuotes: Leave
88+
JavaScriptWrapImports: true
89+
KeepEmptyLinesAtTheStartOfBlocks: true
90+
MacroBlockBegin: ''
91+
MacroBlockEnd: ''
92+
MaxEmptyLinesToKeep: 1
93+
NamespaceIndentation: None
94+
ObjCBinPackProtocolList: Auto
95+
ObjCBlockIndentWidth: 2
96+
ObjCSpaceAfterProperty: false
97+
ObjCSpaceBeforeProtocolList: true
98+
PenaltyBreakAssignment: 2
99+
PenaltyBreakBeforeFirstCallParameter: 19
100+
PenaltyBreakComment: 300
101+
PenaltyBreakFirstLessLess: 120
102+
PenaltyBreakString: 1000
103+
PenaltyBreakTemplateDeclaration: 10
104+
PenaltyExcessCharacter: 1000000
105+
PenaltyReturnTypeOnItsOwnLine: 60
106+
PointerAlignment: Right
107+
ReflowComments: true
108+
SortIncludes: true
109+
SortUsingDeclarations: true
110+
SpaceAfterCStyleCast: false
111+
SpaceAfterLogicalNot: false
112+
SpaceAfterTemplateKeyword: true
113+
SpaceBeforeAssignmentOperators: true
114+
SpaceBeforeCpp11BracedList: false
115+
SpaceBeforeCtorInitializerColon: true
116+
SpaceBeforeInheritanceColon: true
117+
SpaceBeforeParens: ControlStatements
118+
SpaceBeforeRangeBasedForLoopColon: true
119+
SpaceInEmptyBlock: false
120+
SpaceInEmptyParentheses: false
121+
SpacesBeforeTrailingComments: 1
122+
SpacesInAngles: false
123+
SpacesInConditionalStatement: false
124+
SpacesInContainerLiterals: true
125+
SpacesInCStyleCastParentheses: false
126+
SpacesInParentheses: false
127+
SpacesInSquareBrackets: false
128+
SpaceBeforeSquareBrackets: false
129+
Standard: Latest
130+
StatementMacros:
131+
- Q_UNUSED
132+
- QT_REQUIRE_VERSION
133+
TabWidth: 8
134+
UseCRLF: false
135+
UseTab: Never
136+
...

.licenserc.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ header:
2020
- '**/.gitignore'
2121
- '**/.gitmodules'
2222
- '.cargo'
23+
- '.clang-format'
2324
- '.idea'
2425
- '.vscode'
2526
- 'LICENSE'

examples/complex/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
use phper::{
1212
arrays::ZArray,
13-
classes::{StatefulClass, Visibility},
13+
classes::{ClassEntity, Visibility},
1414
functions::Argument,
1515
ini::{ini_get, Policy},
1616
modules::{Module, ModuleContext},
17-
objects::StatefulObj,
17+
objects::StateObj,
1818
php_get_module,
1919
values::ZVal,
2020
};
@@ -73,12 +73,12 @@ pub fn get_module() -> Module {
7373
});
7474

7575
// register classes
76-
let mut foo_class = StatefulClass::new("FooClass");
76+
let mut foo_class = ClassEntity::new("FooClass");
7777
foo_class.add_property("foo", Visibility::Private, 100);
7878
foo_class.add_method(
7979
"getFoo",
8080
Visibility::Public,
81-
|this: &mut StatefulObj<()>, _: &mut [ZVal]| {
81+
|this: &mut StateObj<()>, _: &mut [ZVal]| {
8282
let prop = this.get_property("foo");
8383
Ok::<_, phper::Error>(prop.clone())
8484
},
@@ -87,7 +87,7 @@ pub fn get_module() -> Module {
8787
.add_method(
8888
"setFoo",
8989
Visibility::Public,
90-
|this: &mut StatefulObj<()>, arguments: &mut [ZVal]| -> phper::Result<()> {
90+
|this: &mut StateObj<()>, arguments: &mut [ZVal]| -> phper::Result<()> {
9191
this.set_property("foo", arguments[0].clone());
9292
Ok(())
9393
},

examples/http-client/src/client.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use crate::{errors::HttpClientError, request::REQUEST_BUILDER_CLASS_NAME};
1212
use phper::{
1313
alloc::ToRefOwned,
14-
classes::{ClassEntry, StatefulClass, Visibility},
14+
classes::{ClassEntity, ClassEntry, Visibility},
1515
functions::Argument,
1616
};
1717
use reqwest::blocking::{Client, ClientBuilder};
@@ -20,10 +20,10 @@ use std::{mem::take, time::Duration};
2020
const HTTP_CLIENT_BUILDER_CLASS_NAME: &str = "HttpClient\\HttpClientBuilder";
2121
const HTTP_CLIENT_CLASS_NAME: &str = "HttpClient\\HttpClient";
2222

23-
pub fn make_client_builder_class() -> StatefulClass<ClientBuilder> {
24-
// `new_with_default_state` means initialize the state of `ClientBuilder` as
25-
// `Default::default`.
26-
let mut class = StatefulClass::new_with_default_state(HTTP_CLIENT_BUILDER_CLASS_NAME);
23+
pub fn make_client_builder_class() -> ClassEntity<ClientBuilder> {
24+
// `new_with_default_state_constructor` means initialize the state of
25+
// `ClientBuilder` as `Default::default`.
26+
let mut class = ClassEntity::new_with_default_state_constructor(HTTP_CLIENT_BUILDER_CLASS_NAME);
2727

2828
// Inner call the `ClientBuilder::timeout`.
2929
class
@@ -62,8 +62,9 @@ pub fn make_client_builder_class() -> StatefulClass<ClientBuilder> {
6262
class
6363
}
6464

65-
pub fn make_client_class() -> StatefulClass<Option<Client>> {
66-
let mut class = StatefulClass::<Option<Client>>::new_with_default_state(HTTP_CLIENT_CLASS_NAME);
65+
pub fn make_client_class() -> ClassEntity<Option<Client>> {
66+
let mut class =
67+
ClassEntity::<Option<Client>>::new_with_default_state_constructor(HTTP_CLIENT_CLASS_NAME);
6768

6869
class.add_method("__construct", Visibility::Private, |_, _| {});
6970

examples/http-client/src/errors.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
// See the Mulan PSL v2 for more details.
1010

1111
use phper::{
12-
classes::{ClassEntry, StatefulClass},
12+
classes::{ClassEntity, ClassEntry},
1313
errors::{exception_class, Throwable},
1414
};
1515

1616
/// The exception class name of extension.
1717
const EXCEPTION_CLASS_NAME: &str = "HttpClient\\HttpClientException";
1818

19-
pub fn make_exception_class() -> StatefulClass<()> {
20-
let mut exception_class = StatefulClass::new(EXCEPTION_CLASS_NAME);
19+
pub fn make_exception_class() -> ClassEntity<()> {
20+
let mut class = ClassEntity::new(EXCEPTION_CLASS_NAME);
2121
// The `extends` is same as the PHP class `extends`.
22-
exception_class.extends("Exception");
23-
exception_class
22+
class.extends(exception_class);
23+
class
2424
}
2525

2626
#[derive(Debug, thiserror::Error)]

examples/http-client/src/request.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@
1010

1111
use crate::{errors::HttpClientError, response::RESPONSE_CLASS_NAME};
1212
use phper::{
13-
classes::{ClassEntry, StatefulClass, Visibility},
13+
classes::{ClassEntity, ClassEntry, Visibility},
1414
errors::ThrowObject,
1515
};
1616
use reqwest::blocking::RequestBuilder;
1717
use std::mem::take;
1818

1919
pub const REQUEST_BUILDER_CLASS_NAME: &str = "HttpClient\\RequestBuilder";
2020

21-
pub fn make_request_builder_class() -> StatefulClass<Option<RequestBuilder>> {
22-
let mut class =
23-
StatefulClass::<Option<RequestBuilder>>::new_with_default_state(REQUEST_BUILDER_CLASS_NAME);
21+
pub fn make_request_builder_class() -> ClassEntity<Option<RequestBuilder>> {
22+
let mut class = ClassEntity::<Option<RequestBuilder>>::new_with_default_state_constructor(
23+
REQUEST_BUILDER_CLASS_NAME,
24+
);
2425

2526
class.add_method("__construct", Visibility::Private, |_, _| {});
2627

examples/http-client/src/response.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@
1111
use crate::errors::HttpClientError;
1212
use phper::{
1313
arrays::{InsertKey, ZArray},
14-
classes::{StatefulClass, Visibility},
14+
classes::{ClassEntity, Visibility},
1515
values::ZVal,
1616
};
1717
use reqwest::blocking::Response;
1818
use std::mem::take;
1919

2020
pub const RESPONSE_CLASS_NAME: &str = "HttpClient\\Response";
2121

22-
pub fn make_response_class() -> StatefulClass<Option<Response>> {
23-
let mut class = StatefulClass::<Option<Response>>::new_with_default_state(RESPONSE_CLASS_NAME);
22+
pub fn make_response_class() -> ClassEntity<Option<Response>> {
23+
let mut class =
24+
ClassEntity::<Option<Response>>::new_with_default_state_constructor(RESPONSE_CLASS_NAME);
2425

2526
class.add_method("body", Visibility::Public, |this, _arguments| {
2627
let response = take(this.as_mut_state());

examples/http-server/src/errors.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// See the Mulan PSL v2 for more details.
1010

1111
use phper::{
12-
classes::{ClassEntry, StatefulClass},
12+
classes::{ClassEntity, ClassEntry},
1313
errors::{exception_class, Throwable},
1414
};
1515
use std::error::Error;
@@ -32,8 +32,8 @@ impl From<HttpServerError> for phper::Error {
3232
}
3333
}
3434

35-
pub fn make_exception_class() -> StatefulClass<()> {
36-
let mut exception_class = StatefulClass::new(EXCEPTION_CLASS_NAME);
37-
exception_class.extends("Exception");
38-
exception_class
35+
pub fn make_exception_class() -> ClassEntity<()> {
36+
let mut class = ClassEntity::new(EXCEPTION_CLASS_NAME);
37+
class.extends(exception_class);
38+
class
3939
}

examples/http-server/src/request.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
99
// See the Mulan PSL v2 for more details.
1010

11-
use phper::classes::{StatefulClass, Visibility};
11+
use phper::classes::{ClassEntity, Visibility};
1212

1313
pub const HTTP_REQUEST_CLASS_NAME: &str = "HttpServer\\HttpRequest";
1414

15-
pub fn make_request_class() -> StatefulClass<()> {
16-
let mut class = StatefulClass::new(HTTP_REQUEST_CLASS_NAME);
15+
pub fn make_request_class() -> ClassEntity<()> {
16+
let mut class = ClassEntity::new(HTTP_REQUEST_CLASS_NAME);
1717

1818
class.add_property("header", Visibility::Public, ());
1919
class.add_property("server", Visibility::Public, ());

examples/http-server/src/response.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
use crate::errors::HttpServerError;
1212
use hyper::{header::HeaderName, http::HeaderValue, Body, Response};
1313
use phper::{
14-
classes::{StatefulClass, Visibility},
14+
classes::{ClassEntity, Visibility},
1515
functions::Argument,
1616
};
1717

1818
pub const HTTP_RESPONSE_CLASS_NAME: &str = "HttpServer\\HttpResponse";
1919

20-
pub fn make_response_class() -> StatefulClass<Response<Body>> {
21-
let mut class = StatefulClass::new_with_default_state(HTTP_RESPONSE_CLASS_NAME);
20+
pub fn make_response_class() -> ClassEntity<Response<Body>> {
21+
let mut class = ClassEntity::new_with_default_state_constructor(HTTP_RESPONSE_CLASS_NAME);
2222

2323
class
2424
.add_method("header", Visibility::Public, |this, arguments| {

0 commit comments

Comments
 (0)